在 Word [docx] [Python 3.8.2] 中设置 table 单元格的背景颜色
Setting the background color of a table cell in Word [docx] [Python 3.8.2]
我无法理解如何设置 table 单元格的背景颜色。
我尝试了很多组合,但我唯一能做的就是设置段落样式。
import docx
doc = docx.Document()
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
table = doc.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].add_paragraph(str(qty), "Title 1")
row_cells[0].text_frame.paragraph[0].font.size = Pt(12)
row_cells[1].text = id
row_cells[2].text = desc
doc.save("Testing.docx")
我想将 row_cells[0] 设置为红色背景,但我做不到。
我该怎么做?
官方 python-docx 库仍然不支持该选项。
但是,您可以尝试自己实施它。您要查找的 属性 名为 cell shading located under cell properties。
解决方法:
添加阴影元素(w:shd
)到单元格属性(w:tcPr
).
编写了执行此操作的简单函数:
def _set_cell_background(cell, fill, color=None, val=None):
"""
@fill: Specifies the color to be used for the background
@color: Specifies the color to be used for any foreground
pattern specified with the val attribute
@val: Specifies the pattern to be used to lay the pattern
color over the background color.
"""
from docx.oxml.shared import qn # feel free to move these out
from docx.oxml.xmlchemy import OxmlElement
cell_properties = cell._element.tcPr
try:
cell_shading = cell_properties.xpath('w:shd')[0] # in case there's already shading
except IndexError:
cell_shading = OxmlElement('w:shd') # add new w:shd element to it
if fill:
cell_shading.set(qn('w:fill'), fill) # set fill property, respecting namespace
if color:
pass # TODO
if val:
pass # TODO
cell_properties.append(cell_shading) # finally extend cell props with shading element
如果需要,请随意扩展其他属性。
所以根据你的例子,一旦你有了你的 table,在保存文档之前,添加这一行:
.
.
_set_cell_background(table.rows[0].cells[0], 'FF0000')
doc.save("Testing.docx")
此外,请随时通过在此处注册此新元素来为官方图书馆做出贡献:https://github.com/python-openxml/python-docx/blob/master/docx/oxml/table.py#L753。 :)
希望这对您有所帮助,
我无法理解如何设置 table 单元格的背景颜色。
我尝试了很多组合,但我唯一能做的就是设置段落样式。
import docx
doc = docx.Document()
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
table = doc.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].add_paragraph(str(qty), "Title 1")
row_cells[0].text_frame.paragraph[0].font.size = Pt(12)
row_cells[1].text = id
row_cells[2].text = desc
doc.save("Testing.docx")
我想将 row_cells[0] 设置为红色背景,但我做不到。
我该怎么做?
官方 python-docx 库仍然不支持该选项。 但是,您可以尝试自己实施它。您要查找的 属性 名为 cell shading located under cell properties。
解决方法:
添加阴影元素(w:shd
)到单元格属性(w:tcPr
).
编写了执行此操作的简单函数:
def _set_cell_background(cell, fill, color=None, val=None):
"""
@fill: Specifies the color to be used for the background
@color: Specifies the color to be used for any foreground
pattern specified with the val attribute
@val: Specifies the pattern to be used to lay the pattern
color over the background color.
"""
from docx.oxml.shared import qn # feel free to move these out
from docx.oxml.xmlchemy import OxmlElement
cell_properties = cell._element.tcPr
try:
cell_shading = cell_properties.xpath('w:shd')[0] # in case there's already shading
except IndexError:
cell_shading = OxmlElement('w:shd') # add new w:shd element to it
if fill:
cell_shading.set(qn('w:fill'), fill) # set fill property, respecting namespace
if color:
pass # TODO
if val:
pass # TODO
cell_properties.append(cell_shading) # finally extend cell props with shading element
如果需要,请随意扩展其他属性。
所以根据你的例子,一旦你有了你的 table,在保存文档之前,添加这一行:
.
.
_set_cell_background(table.rows[0].cells[0], 'FF0000')
doc.save("Testing.docx")
此外,请随时通过在此处注册此新元素来为官方图书馆做出贡献:https://github.com/python-openxml/python-docx/blob/master/docx/oxml/table.py#L753。 :)
希望这对您有所帮助,