栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python-docx】添加表格、合并单元格、设置样式

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python-docx】添加表格、合并单元格、设置样式

1.表格 1.1 添加表格

添加表格很简单,只需要调用一下add_table()即可,返回一个Table对象,参数可以指定行、列、样式

from docx import Document

doc = Document()
# 添加一个5行3列的表格,样式是网格实线
table = doc.add_table(5, 3, style="Table Grid")
doc.save('./test.docx')

1.2 添加行列
from docx import Document
from docx.shared import Cm, RGBColor, Pt
...
table.add_row()  # 在最下面添加一行
table.add_column(Pt(25))  # 在最右边添加一列并指定宽度为25磅
1.3 表格样式
...
table.cell(1, 2).text = "冰冷的希望"
table.style.font.size = Pt(15)  # 字体大小15磅
table.style.font.color.rgb = RGBColor.from_string("6495ED")  # 字体颜色
table.style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT  # 左对齐
2.行列对象

首先是一个表格(Table),表格里有行(Row)和列(Column),行或列里有单元格(Cell)
python-docx中用_Row和_Column分别代表行和列,,用_Rows和_Columns表示多行多列,可以使用Table对象的rows和columns属性获取所有行列,如果想要访问行列里的单元格,可以进一步遍历

from docx import Document

doc = Document()
table = doc.add_table(5, 3, style="Table Grid")
print(table.rows)  # 获取所有行
print(table.columns)  # 获取所有列
# 按行遍历单元格
for row in table.rows:
    for cell in row.cells:
        print(cell)
# 按列遍历单元格
for col in table.columns:
    for cell in col.cells:
        print(cell)

_Rows和_Columns对象里有一个table属性可以返回所属的表格对象

doc = Document()
table = doc.add_table(5, 3, style="Table Grid")

for row in table.rows:
    print(row.table)

for col in table.columns:
    print(col.table)

通过上面的遍历可以发现其实_Rows和_Columns就是分别包含Row和Column的可迭代对象,可以通过遍历分别取出Row和Column对象,而Row和Column对象也很简单,两者的属性一样的,如下

for row in table.rows:
    print(row.cells)  # 所有单元格
    print(row.height)  # 高度
    # 第2行高度改为30磅
    if row._index == 2:
        row.height = Pt(30)
    print(row.height_rule)  # 指定高度的规则
    print(row.table)  # 当前表格对象
    print(row._index)  # 下标
3.单元格对象 3.1 获取Cell对象

python-docx中用Cell代表单元格,获取单元格对象的方式除了上面的嵌套循环,还可以通过下标获取

doc = Document()
table = doc.add_table(5, 3, style="Table Grid")

# 获取第1行第3列的单元格(下标从0开始)
cell1 = table.cell(0, 2)
3.2 修改单元格文本

如果想要修改单元格的文本,可以直接修改Cell对象的text属性,其实它也是获取单元格的段落然后修改,所以有两种方式

from docx import Document

doc = Document()
table = doc.add_table(5, 3, style="Table Grid")

# 获取第1行第3列的单元格(下标从0开始)
cell1 = table.cell(0, 2)
cell1.text = "冰冷的希望"

cell2 = table.cell(1, 2)
paragraph= cell2.paragraphs[0]
run = paragraph.add_run("冰冰很帅")
3.3 合并单元格
...
cell3 = table.cell(2, 1)
cell4 = table.cell(3, 2)
cell3.merge(cell4)
3.4 单元格样式

可以设置整个表格的样式,也可以单独设置单元格的样式,优先显示单元格样式

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Cm, RGBColor, Pt

...

cell2 = table.cell(1, 2)
paragraph = cell2.paragraphs[0]
run = paragraph.add_run("冰冰很帅")

# 设置单元格样式
run.font.color.rgb = RGBColor.from_string("00FFFF")  # 字体颜色
run.font.size = Cm(1)  # 字体大小,1厘米
paragraph.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 居中对齐

# 设置表格样式,但单元格样式优先显示
table.style.font.size = Pt(15)  # 字体大小15磅
table.style.font.color.rgb = RGBColor.from_string("6495ED")  # 字体颜色
table.style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT  # 左对齐
3.5 宽高度
...
# 以同一行或同一列的最大值为准
table.cell(0, 0).width = Cm(3)
table.rows[0].height = Cm(2)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/987170.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号