I'm using python 2.7 Python-docx
to try and modify the contents of cells in a table that is in a Microsoft docx document. I opened the document as xml so I could get a look at where things are and try to get values so I can reference them. Below is what I found.
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="TableGrid"/>
<w:tblW w:w="0" w:type="auto"/>
<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="1345"/>
<w:gridCol w:w="3148"/>
<w:gridCol w:w="3148"/>
<w:gridCol w:w="3149"/>
</w:tblGrid>
<w:tr w:rsidR="002C543C" w14:paraId="4C33FE0D" w14:textId="77777777" w:rsidTr="009E290C">
<w:trPr>
<w:cantSplit/>
<w:trHeight w:hRule="exact" w:val="1080"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="1345" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="4497FDDB" w14:textId="77777777" w:rsidR="002C543C" w:rsidRDefault="002C543C">
<w:pPr>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
</w:pPr>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="3148" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="15F285F0" w14:textId="77777777" w:rsidR="002C543C" w:rsidRDefault="002C543C" w:rsidP="009E290C">
<w:pPr>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
</w:pPr>
</w:p>
<w:p w14:paraId="140917B0" w14:textId="77777777" w:rsidR="009E290C" w:rsidRPr="001261E4" w:rsidRDefault="009E290C" w:rsidP="009E290C">
<w:pPr>
<w:jc w:val="center"/>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:b/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="001261E4">
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:b/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
<w:t>this is cell (1, 2)</w:t>
</w:r>
</w:p>
So using the above xml as reference below is what I tried implement to update the cell (0,0).
from docx import Document
from docx.shared import Inches
from docx.oxml.table import CT_Tbl
from docx.oxml.text.paragraph import CT_P
from docx.table import Table
from docx.text.paragraph import Paragraph
f = open('filename.docx')
doc = Document(f)
table_to_update = Table('04A0', doc) # value from above <w:tblLook w:val="04A0"
cell = table_to_update.cell(0, 0) # this produces the error
#cell.text = 'can we add something'
This code produces the following error
AttributeError: 'str' object has no attribute 'col_count'
So I assume it's from the 04A0
value I'm sending in. So first off I'm trying to find how to reference the table that I want to modify. From there I want to find the cell within that table and modify that. I've been looking for examples of this but haven't been able to find any.
Table
's constructor accepts <w:tbl>
XML subtree and not the string id of the table (that's why it fails). Furthermore what you think is id is actually:
Specifies what aspects of the table styles should be included. This is a bitmask of options: 0x0020=Apply header row formatting; 0x0040=Apply last row formatting; 0x0080=Apply header column formatting; 0x0100=Apply last column formatting.
You can get list of tables in the document by using following code:
doc = Document('filename.docx')
print(doc.tables)
Then you have to understand, which of the tables you need to modify (by position in the list or headers of the table or whatever applies). I will use the first table for simplicity. When you have you Table
object you can modify cell values by doing:
table = doc.tables[0]
table.cell(0, 0).text = 'new value'
Then you can save updated document:
doc.save('filename_updated.docx')