Comments (16)
What you need to do is open a document in your Document() call, not just use the default.
- Create a blank document in Word and save it as 'template.docx'.
- Customize one of the table styles in 'template.docx' to look the way you want.
- Create a new table while still in Word, and apply the customized table style to it
- After that you can delete the table and save the file again.
After that you start a new document in python-docx using document = Document('template.docx')
and it will use the file you customized. The table style you customized will be available to apply to tables you make with python-pptx. The table style name is the same as it is in Word, with all spaces removed. For example, 'Light Shading - Accent 1' becomes 'LightShading-Accent1'.
from python-docx.
table = document.add_table(rows, cols)
table.style = 'TableGrid'
http://python-docx.readthedocs.org/en/latest/api/table.html#docx.table.Table.style
from python-docx.
You have to set it using Document object which contains all the styles. https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html#table-styles-in-default-template
Like this
document = Document()
table.style = document.styles['Table Grid']
from python-docx.
What you need to do is open a document in your Document() call, not just use the default.
- Create a blank document in Word and save it as 'template.docx'.
- Customize one of the table styles in 'template.docx' to look the way you want.
- Create a new table while still in Word, and apply the customized table style to it
- After that you can delete the table and save the file again.
After that you start a new document in python-docx using
document = Document('template.docx')
and it will use the file you customized. The table style you customized will be available to apply to tables you make with python-pptx. The table style name is the same as it is in Word, with all spaces removed. For example, 'Light Shading - Accent 1' becomes 'LightShading-Accent1'.
It has been too long, I scoured the internet for an answer. Thank you
from python-docx.
I'm sorry,i just understood what's the alpha version mean.
so , Im waiting beta version.
from python-docx.
Yes,I konw that ,But if i want set color to table , how can i do that, for example
table = document.add_table(rows, cols)
table.style = 'TableGrid'
table.row[0].style = "borderColor:red;background-color:gray"
and i need set font style , like ,color ,size ,and so on.
from python-docx.
There's currently no way to define table styles within python-docx. However if the "template" document you start with contains table styles, you can use them by name as above.
So if you want a customized table style, just customize it in the template document you're using and apply it by name to the table.
from python-docx.
Thanks scanny,
Actually i just try to use "template " ,
I use microsoft Word open 'default.docx' file ,and add something ,
But doesn't work , so could you give some advice ?
from python-docx.
paste in the code you're using to open the document so I can see, like:
document = Document('base.docx')
from python-docx.
def create_content_docx(proj_id,svgs,version):
store_path = report_utils.get_archive_dir(proj_id,version)
proj = survey_utils.get_project(proj_id)
file_name = "/%s_c.docx " % proj.title_as_txt
document = Document()
document.add_heading(proj.title_as_txt, 0)
document.add_paragraph(proj.title_as_txt , style="Title1")
question_style = "Quote"
proj_dict = get_struct_dict(proj_id,version)
question_list = proj_dict.get("question_list")
for index,question in enumerate(question_list):
question_id = question.get("id")
question_cid = question.get("cid")
question_title = question_cid+":"+question.get("title")
question_type = question.get("question_type")
document.add_paragraph(question_title , style=question_style)
if question_type in [4,5,7,100]:
matrix_content(document,question)
else:
normal_question(document,question)
document.add_paragraph("")
document.save(store_path+file_name)
return get_docx_content_stream(proj_id,version)
def matrix_content(document,question):
question_type = question.get("question_type")
position_content = ""
table = document.add_table(rows=len(question.get("matrixrow_list"))+1, cols=len(question.get("option_list"))+1,style="TableGrid")
if question_type == enums.QUESTION_TYPE_MATRIX_SCORE:
max_num = question.get("custom_attr").get("max_answer_num")
position_content = u"%s" % (u" ★ " * int(max_num))
if question_type == enums.QUESTION_TYPE_MATRIX_BLANK:
position_content = "____________"
if question_type == enums.QUESTION_TYPE_MATRIX_SINGLE:
position_content = u" ○ "
if question_type == enums.QUESTION_TYPE_MATRIX_MULTIPLE:
position_content = u" □ "
for i,row in enumerate(table.rows):
if i == 0:
option_cells = table.rows[0].cells
for j,option in enumerate(question.get("option_list")):
if j == 0:
option_cells[j].text = ""
option_cells[j+1].text = option.get("title")
else:
matrix_cells = table.rows[i].cells
matrix_list = question.get("matrixrow_list")
for j,m_cell in enumerate(matrix_cells):
if j == 0:
m_cell.text = matrix_list[i-1].get("title")
continue
m_cell.text = position_content
def normal_question(document,question):
question_type = question.get("question_type")
custom_attr = question.get("custom_attr")
option_style = ""
for index,option in enumerate(question.get("option_list")):
option_content = option.get("title")
if question_type == enums.QUESTION_TYPE_SCORE:
max_num = custom_attr.get("max_answer_num")
stars = u"★ " * int(max_num)
option_content += stars
if question_type in (enums.QUESTION_TYPE_BLANK,enums.QUESTION_TYPE_MULTIPLE_BLANK):
option_content += "____________"
sign = ""
if question_type == enums.QUESTION_TYPE_SINGLE:
sign = u" ○ "
if question_type == enums.QUESTION_TYPE_MULTIPLE:
sign = u" □ "
content = "%s %s" % (sign,option_content)
# add feature
document.add_paragraph(content,option_style)
from python-docx.
Because, My Project is website ,so the code maybe too long , so give me some information about how to set 'template ' , that's okay.
from python-docx.
thanks very much!
from python-docx.
glad it worked out Keith :)
from python-docx.
I want to change font size of table content. I tried lots.
from python-docx.
It seems the font of the previous paragraph is reused (which is a problem from Word itself). I used this not-so-beautiful workaround:
- add a paragraph
- set its font size
- remove the paragraph
Then, the next table will have the deleted paragraph's font size.
Code:
doc.add_paragraph('')
paragraph = doc.paragraphs[-1]
paragraph.style.font.size = Pt(font_size)
p = paragraph._element
p.getparent().remove(p)
p._p = p._element = None
doc.add_table(...)
from python-docx.
The above solution works fine if you start with the template file, but I find myself in a situation where I'm injecting tables into an existing .docx file (which doesn't stay static and can change, so I can't do my template creation trick there), meaning that I use a separate Document() to create the table and then inject it to the main file. That means that what ever I do with the styles of the table file, all of that gets lost once the table goes to the main file and I'm left with just the standard Table Grid as my choice. Are there any other options for me here or am I doomed? I also need to do things such as color the background of a cell depending on the content, also something that would get lost after injecting the tables to the main file.
Are there any alternatives or longer solutions I could look into, or is this the pinnacle of what python can do for me when it comes to working with .docx files? Many thanks to whoever can provide some insights.
from python-docx.
Related Issues (20)
- The cell's vMerge attribute may be incorrect HOT 1
- Highlight particular word in python docx HOT 1
- support more keys in nsmap. HOT 1
- pip Install python-docx==1.1.1 raise error in python 3.12, ERROR: Failed building wheel for lxml<=4.9.2,>=3.1.0 (in mac os) HOT 8
- DocumentPart' object has no attribute '_rels'. HOT 1
- Non compatibility of new update 1.1.1 with python-docx-template HOT 6
- Remove "generated by python-docx" from description tag HOT 5
- track-changes in python-docx HOT 5
- `doc.paragraphs` seems not including contents inside a `<mc:AlternateContent>` tag HOT 2
- [Feature] Support EMF image
- customXML Error HOT 4
- Chinese fonts Only the non-Chinese parts are valid
- Can not read an empty docx...please fix it.
- 打开空的docx文档时报错
- Inline support for SVG file stream HOT 3
- How to add internal top and bottom table cell spacings?
- Inserting a new page before last page in the word document using python HOT 1
- Auto refresh Table of Contents using docx HOT 1
- OSS-Fuzz Integration
- Highlight certain words of a paragraph to bold
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-docx.