How To Get Cell Background Color In Python-docx?
I'm trying to read data from MS Word table using python-docx. There is a way to set background color of a table cell: tcPr = cell._tc.get_or_add_tcPr() shd = OxmlElement('w:shd') s
Solution 1:
As scanny proposed, I used parsing cell._tc.xml:
pattern = re.compile('w:fill=\"(\S*)\"')
match = pattern.search(cell._tc.xml)
result = match.group(1)
If there is data on color it returns either "auto" or hex code of background color which can be converted to RGB.
Solution 2:
As scanny said, you should first be sure of the element/property you are looking for.
But to read the value of this element you should rather use the find method.
Ex:
cell._tc.get_or_add_tcPr().get(qn('w:shd')) #ReturnsNone
cell._tc.get_or_add_tcPr().find(qn('w:shd')) #Returns <Element {http://schemas.openxmlformats.org/wordprocessingml/2006/main}shd at ...>
Post a Comment for "How To Get Cell Background Color In Python-docx?"