Reputation: 483
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")
shd.set(qn("w:fill"), rgb2hex(*color))
tcPr.append(shd)
My task is contrary, I need to get the existing color. I'm not skilled in xml and I tried this:
cell = table.cell(row, col)
tcPr = cell._tc.get_or_add_tcPr().get(qn('w:shd'))
How ever it returns me None for each read cell regardless of its color.
Upvotes: 2
Views: 2594
Reputation: 111
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')) #Returns None
cell._tc.get_or_add_tcPr().find(qn('w:shd')) #Returns <Element {http://schemas.openxmlformats.org/wordprocessingml/2006/main}shd at ...>
Upvotes: 2