python docx设置背景色表格单元格

python docx设置背景色表格单元格,python,python-docx,Python,Python Docx,要设置表中单元格的背景色,我使用以下代码: doc.add_paragraph('') t1 = doc.add_table(rows=7, cols=2) t1.style = 'TableGrid' for row in range(7): cell = t1.cell(row, 0) cell._tc.get_or_add_tcPr().append(shading_elm_green) 唯一的问题是结果如下: 但是我希望所有的单元格都有背景色。为什么不设置所有单元格。

要设置表中单元格的背景色,我使用以下代码:

doc.add_paragraph('')
t1 = doc.add_table(rows=7, cols=2)
t1.style = 'TableGrid'
for row in range(7):
    cell = t1.cell(row, 0)
    cell._tc.get_or_add_tcPr().append(shading_elm_green)
唯一的问题是结果如下:

但是我希望所有的单元格都有背景色。为什么不设置所有单元格。另外,当我创建许多表格时,所有单元格都是清晰的,最后一个表格的最后一个单元格只设置了


我做错了什么?请我正在寻找一个解决方案很多天了

您需要为每个单元格创建一个新的
shading\u elm\u green
元素。每次在当前代码中分配它时,您只是将它从一个单元格移动到下一个单元格。这就是为什么它最终会结束

lxml
API在这种情况下有点违反直觉(除非您考虑自己将如何做:)。例如,使用
.append()
将现有元素指定为另一个元素的子元素时,
lxml
会将该元素移动为该另一个元素的子元素。如果将其附加到其他元素,则会将其移动到该元素。指定的元素不会自动“克隆”或类似的内容。它只能生活在一个地方,而那个地方是你最后一次“放置”它的地方


您不会显示元素创建代码,但不管它是什么,请将它插入到最后一行的下一行,事情应该按照您所期望的方式进行。

给那些想知道的人举个例子


from docx import Document
from docx.shared import Inches
from docx.enum.table import WD_ALIGN_VERTICAL
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.enum.style import WD_STYLE
from docx.dml.color import ColorFormat
from docx.enum.dml import MSO_COLOR_TYPE
from docx.enum.text import WD_COLOR_INDEX
from docx.enum.text import WD_COLOR
from docx.shared import Pt
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml

document = Document()


document.add_heading('Document Title', 0)

table = document.add_table(1, 11)
table.style = 'Table Grid'
table.cell(0,1).merge(table.cell(0,4))
table.cell(0,6).merge(table.cell(0,7))
table.cell(0,9).merge(table.cell(0,10))

table.cell(0, 0).paragraphs[0].add_run("Name").bold = True
table.cell(0, 5).paragraphs[0].add_run("Offset").bold = True

table.cell(0, 1).paragraphs[0].add_run("5566")
table.cell(0, 6).paragraphs[0].add_run("never die")
table.cell(0, 9).paragraphs[0].add_run("1")
for i in range(11):
    table.cell(0, i).paragraphs[0].alignment  = WD_ALIGN_VERTICAL.CENTER

    shading_elm = parse_xml(r'<w:shd {} w:fill="D9D9D9"/>'.format(nsdecls('w')))
    #shading must create every time
    table.cell(0, i)._tc.get_or_add_tcPr().append(shading_elm)



document.add_page_break()

document.save('demo2.docx')

从docx导入文档
从docx.shared导入英寸
从docx.enum.table导入WD_ALIGN_VERTICAL
从docx.enum.table导入WD_table_对齐
从docx.enum.style导入WD_样式
从docx.dml.color导入ColorFormat
从docx.enum.dml导入MSO_颜色_类型
从docx.enum.text导入WD_颜色_索引
从docx.enum.text导入WD_颜色
从docx.shared导入Pt
从docx.oxml.ns导入nsdecls
从docx.oxml导入解析xml
文件=文件()
文档。添加标题(“文档标题”,0)
表格=文件。添加表格(1,11)
table.style='table Grid'
表.单元(0,1).合并(表.单元(0,4))
表.单元(0,6).合并(表.单元(0,7))
表.单元(0,9).合并(表.单元(0,10))
table.cell(0,0).段落[0].添加运行(“名称”).bold=True
table.cell(0,5).段落[0].添加运行(“偏移量”)。粗体=真
表.单元格(0,1).段落[0].添加运行(“5566”)
表.cell(0,6).段落[0].添加\u run(“永不消亡”)
表.单元格(0,9).段落[0].添加运行(“1”)
对于范围(11)中的i:
table.cell(0,i).段落[0].对齐=WD\u ALIGN\u VERTICAL.CENTER
shading_elm=parse_xml(r''。格式(nsdecls('w'))
#每次都必须创建着色
table.cell(0,i)。\u tc.get\u或\u add\u tcPr().append(着色\u elm)
文档。添加页眉()
document.save('demo2.docx')

非常感谢斯坎尼。在我尝试过的数百万种选择中,那不是其中之一!