pythondocx第一行表格背景灰色_python docx设置背景色选项卡

本文提供了一个示例,展示如何使用python-docx库为Word文档中的表格第一行设置背景颜色。通过导入相关模块并创建Document对象,添加表格、合并单元格,并设置特定单元格的对齐方式和内容。最后,通过插入XML元素为第一行单元格添加背景色。
摘要由CSDN通过智能技术生成

给那些想知道的人举个例子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''.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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值