For editing .docx files in python, python-docx exposes some properties but not others. Text properties like bold, italics etc can easily be set, but cell properties like background shading and borders can't AFAIK.
What you can do in python-docx is to view the xml of the cell using the .xml attribute. So cell._tc.xml reveals that cell properties are held in a <w:tcPr> ... </w:tcPr> block. This can be a nested structure, for example
<w:tcPr>
<w:tcW w:w="1139" w:type="dxa"/>
<w:tcBorders>
<w:left w:val="single" w:sz="4" w:space="0" w:color="000000"/>
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="000000"/>
</w:tcBorders>
<w:shd w:fill="FFDBB6" w:val="clear"/>
</w:tcPr>
I would like to copy the exact cell properties from one cell to another cell. I've tried:
- simply copying the xml
<w:tcPr>block from one cell to the other. However, the.xmlattribute is read only -python-docxdoesn't allow setting xml directly. - looping through the source cell's xml structure recursively and setting those properties in the destination cell. So something on the lines of
for child in source_cell._tc.get_or_add_tcPr().iterchildren():
dest_cell._tc.get_or_add_tcPr().insert(child)
but that is raising TypeError: Argument must be bytes or unicode, got 'CT_TcPr'.
How can I achieve this?
Here is a function which copies the properties from one cell to another:
There is a related discussion here - https://github.com/python-openxml/python-docx/issues/205