目录
页眉页脚
表格
表格样式
插入表格
插入行/列
合并单元格
单元格
页眉页脚
页眉页脚操作需要访问文件的section,可通过添加页脚来添加页码。
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_ALIGN_PARAGRAPH, WD_COLOR_INDEX, WD_COLOR
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT,WD_TABLE_DIRECTION
from docx.shared import Inches, Cm, Pt, RGBColordocx_file = Document()section = docx_file.sections[0]
header = section.header.paragraphs[0] # 获取页眉对象
footer = section.footer.paragraphs[0] # 获取页脚对象# 添加页眉
header.text = "这里是页眉"
header.style.font.size = Pt(8)
header.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT# 添加页脚
footer.text = '这是页脚'
footer.style.font.size = Pt(16)
footer.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER# 页眉页脚添加表格
headers = section.header
table = headers.add_table(rows=3, cols=3, width=Cm(4.0))
table.cell(0, 0).text = '姓名'
table.cell(0, 1).text = '年龄'
table.cell(0, 2).text = '籍贯'
支持追加的方式创建内容(.add_run())
section = docx_file.sections[0]
header = section.header.paragraphs[0] # 获取页眉对象
footer = section.footer.paragraphs[0] # 获取页脚对象text_run = header.add_run()
text_run.text = '追加的内容'img_run = header.add_run()
img_run.add_picture(r'C:\Users\Administrator\Desktop\testfile\测试图片\8.png', width=Inches(1),height=Inches(.5)) # 可添加参数修改图片大小
表格
表格样式
官方预定义样式的取值
table_type = ['Colorful Grid', 'Colorful Grid Accent 1', 'Colorful Grid Accent 2','Colorful Grid Accent 3', 'Colorful Grid Accent 4', 'Colorful Grid Accent 5', 'Colorful Grid Accent 6','Colorful List', 'Colorful List Accent 1', 'Colorful List Accent 2', 'Colorful List Accent 3','Colorful List Accent 4', 'Colorful List Accent 5', 'Colorful List Accent 6', 'Colorful Shading','Colorful Shading Accent 1', 'Colorful Shading Accent 2', 'Colorful Shading Accent 3','Colorful Shading Accent 4', 'Colorful Shading Accent 5', 'Colorful Shading Accent 6', 'Dark List','Dark List Accent 1', 'Dark List Accent 2', 'Dark List Accent 3', 'Dark List Accent 4','Dark List Accent 5', 'Dark List Accent 6', 'Light Grid', 'Light Grid Accent 1', 'Light Grid Accent 2','Light Grid Accent 3', 'Light Grid Accent 4', 'Light Grid Accent 5', 'Light Grid Accent 6', 'Light List','Light List Accent 1', 'Light List Accent 2', 'Light List Accent 3', 'Light List Accent 4','Light List Accent 5', 'Light List Accent 6', 'Light Shading', 'Light Shading Accent 1','Light Shading Accent 2', 'Light Shading Accent 3', 'Light Shading Accent 4', 'Light Shading Accent 5','Light Shading Accent 6', 'Medium Grid 1', 'Medium Grid 1 Accent 1', 'Medium Grid 1 Accent 2','Medium Grid 1 Accent 3', 'Medium Grid 1 Accent 4', 'Medium Grid 1 Accent 5', 'Medium Grid 1 Accent 6','Medium Grid 2', 'Medium Grid 2 Accent 1', 'Medium Grid 2 Accent 2', 'Medium Grid 2 Accent 3','Medium Grid 2 Accent 4', 'Medium Grid 2 Accent 5', 'Medium Grid 2 Accent 6', 'Medium Grid 3','Medium Grid 3 Accent 1', 'Medium Grid 3 Accent 2', 'Medium Grid 3 Accent 3', 'Medium Grid 3 Accent 4','Medium Grid 3 Accent 5', 'Medium Grid 3 Accent 6', 'Medium List 1', 'Medium List 1 Accent 1','Medium List 1 Accent 2', 'Medium List 1 Accent 3', 'Medium List 1 Accent 4', 'Medium List 1 Accent 5','Medium List 1 Accent 6', 'Medium List 2', 'Medium List 2 Accent 1', 'Medium List 2 Accent 2','Medium List 2 Accent 3', 'Medium List 2 Accent 4', 'Medium List 2 Accent 5', 'Medium List 2 Accent 6','Medium Shading 1', 'Medium Shading 1 Accent 1', 'Medium Shading 1 Accent 2', 'Medium Shading 1 Accent 3','Medium Shading 1 Accent 4', 'Medium Shading 1 Accent 5', 'Medium Shading 1 Accent 6', 'Medium Shading 2','Medium Shading 2 Accent 1', 'Medium Shading 2 Accent 2', 'Medium Shading 2 Accent 3','Medium Shading 2 Accent 4', 'Medium Shading 2 Accent 5', 'Medium Shading 2 Accent 6', 'Table Grid']
测试打印所有表格样式
for tp in table_type:docx_file.add_paragraph(text=f'表格样式测试{tp}')table_demo = docx_file.add_table(rows=3, cols=4, style=tp)table_demo.cell(0, 0).text = f'{tp}表格样式'docx_file.add_paragraph(text='\n')
插入表格
docx_table = docx_file.add_table(rows=3, cols=8, style='Light Shading Accent 6')
docx_table.autosize = False # 自动调整大小设置
print(len(docx_table.rows)) # 获取行数
print(len(docx_table.columns)) # 获取列数# 设置表格内容样式(表格内的所有单元格文本样式)
docx_table.style.font.size = Pt(15)
docx_table.style.font.color.rgb = RGBColor(100, 100, 50)
docx_table.style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
插入行/列
# 插入行和列(插入到最后一行或最后一列)
docx_table.add_row()
docx_table.add_column(width=Cm(8)) # 添加的列可设置列宽# 设置行高列宽
docx_table.columns[0].wigth = Cm(.5)
docx_table.rows[0].height = Cm(5)
合并单元格
合并单元格,使用单元格对象的merge方法,起始单元格.merge(终止单元格对象)
docx_table.cell(1, 1).merge(docx_table.cell(3, 3)) # 合并2行2列到4行4列的所有单元格
通过循环的方式合并某一行或某一列的操作
r = len(docx_table.rows)
c = len(docx_table.columns)
cells_to_merge = [] # 创建一个空列表
for i in range(c): # 根据列,将需要合并的行的所有列添加到列表中cells_to_merge.append(docx_table.cell(0, i)) # 将第一行的所有单元格对象添加到列表中
for cell in cells_to_merge: # 使用merge方法合并需要合并的单元格(循环遍历合并获取的所有要合并的单元格对象进行合并操作)cells_to_merge[0].merge(cell)
单元格
# 单元格对象获取及设置宽高和内容
cell = docx_table.cell(2, 2) # 获取单元格对象
# 设置行高列宽(以同一行或同一列的最大值为准)
cell.width = Cm(3) # 设置单元格宽度
cell.height = Cm(3) # 设置单元格宽度
cell.text = '写入数据到单元格测试'# 遍历行或列操作
for r, row in enumerate(docx_table.rows): # 遍历行docx_table.rows;遍历列docx_table.columns# print(row.table) # _Rows和_Columns对象里有一个table属性可以返回所属的表格对象# print(row.height) # 获取行高# print(row._index) # 获取行索引IDfor c, cell in enumerate(row.cells): # 迭代所有单元格cell.text = str(r * c) # 测试只能是str型,int型时会报错# 单元格内容样式设置(单个单元格独立设置文本内容样式)
cell2 = docx_table.cell(2, 2)
cell_paragraphs = cell2.paragraphs[0]
cell_style = cell_paragraphs.add_run('样式设置')
cell_style.font.size = Pt(40)
cell_style.font.color.rgb = RGBColor(200, 200, 100)