• 使用python-docx完成word操作


    Table of Contents

    from docx import Document
    import os 
    os.chdir('D:\docx_test')
    
    • 1
    • 2
    • 3

    打开存储(另存为)文件

    document = Document()   # 创建一个空的文档
    document.save('test.docx')  # 保存文档
    document = Document('test.docx')  # 打开文档 
    document.save('test1.docx')  #  另存为文档
    
    • 1
    • 2
    • 3
    • 4

    增加标题

    document = Document('test.docx')  # 打开文档 
    document.add_heading('这是一个标题',level= 0)  # 增加一级标题
    document.add_heading('这是一个小标题',level= 1)  # 增加二级标题 
    
    • 1
    • 2
    • 3

    增加段落和文字

     # add_paragraph 增加段落 可以用|n和|t等
    paragraph1 = document.add_paragraph("这是第1个段落。\n 这里换行",) 
    paragraph2 = document.add_paragraph("这是第2个段落") 
    # insert_paragraph_before 在段落之前插入新的一段 
    paragraph2.insert_paragraph_before('之前插入一段')
    # 清除段落
    paragraph2.clear()
    document.save('test.docx')  # 保存文档
    
    document = Document()   # 创建一个空的文档
    paragraph1 = document.add_paragraph()
    paragraph1.add_run('\n这是段落的主题内容').italic = True  # 设置斜体 
    paragraph1.add_run('\n这是段落的主题内容').bold = True  # 设置粗体
    paragraph1.add_run('\n这是段落的主题内容').underline= True  # 设置粗体
    document.save('test.docx')  # 保存文档
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    字体样式设置

    from docx.shared import Inches  #定义英尺
    from docx.shared import Pt  #定义像素大小
    from docx.shared import RGBColor  # 字体颜色
    from docx.enum.text import WD_ALIGN_PARAGRAPH  # 对其方式 
    from docx.oxml.ns import qn # 导入qn  不导入qn字体不能修改中文字体 
    document = Document()   
    # 创建自定义段落样式(第一个参数为样式名, 第二个参数为样式类型, 1为段落样式, 2为字符样式, 3为表格样式)
    # 如同时设置斜体和粗体,应该使用style
    # 字体样式设置
    UserStyle1 = document.styles.add_style('UserStyle1', 1)
    UserStyle1.font.size = Pt(20) # 设置字体尺寸
    UserStyle1.font.color.rgb = RGBColor(216, 11, 123) # 设置字体颜色
    UserStyle1.font.name = "Times New Roman" # 设置英文字体
    UserStyle1._element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋')  # 设置中文字体 
    UserStyle1.font.bold = True 
    UserStyle1.font.underline = True
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    段落样式设置

    'alignment':文本对齐方式,
    'element',
    'first_line_indent':首行缩进,
    'keep_together':段中不分页,
    'keep_with_next':和下一段保持同一页,
    'left_indent':段落到页面左边距距离,
    'line_spacing':行间距,
    'line_spacing_rule',
    'page_break_before':与前端分页,
    'part',
    'right_indent',
    'space_after':段前间距,
    'space_before':段后间距,
    'tab_stops',
    'widow_control'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    UserStyle1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.LEFT # 居中文本  LEFT:左对齐 CENTER:居中对齐 RIGHT:右对齐等
    UserStyle1.paragraph_format.line_spacing = Pt(30) # 行间距 距离有CM厘米 Pt磅数 和 Inches英尺 
    UserStyle1.paragraph_format.first_line_indent = Pt(40) # 首行缩进 如果缩进2字符 可以用字体磅数*2 
    
    paragraph2 = document.add_paragraph('加强贷款资金管理。贷款资金发放、本息回收代扣、止付等关键环节由银行自主决策,指令由银行发起。采用自主支付的,资金应直接发放至借款人银行账户;采用受托支付的,商业银行应当履行受托支付责任,将贷款资金最终支付给符合借款人合同约定用途的交易对象。商业银行应当自主完整保留贷款资金发放、本息回收等账户流水信息,主动加强贷款资金管理,并采取有效措施对贷款用途进行监测,确保贷款资金安全,防范合作机构截留、汇集、挪用',style = UserStyle1)
    #
    document.save('test.docx')  # 保存文档
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    document中样式修改

    文档中的样式库可以在document.styles中查看 这里对默认字体进行修改

    document = Document()   
    document.styles['Normal'].font.name = u'宋体'
    document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
    document.styles['Normal'].font.size = Pt(20)
    document.add_paragraph('新的段落')
    document.save('test.docx')  # 保存文档
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    有序和无序类表

    document = Document()   
    document.styles['Normal'].font.name = u'宋体'
    document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
    document.styles['Normal'].font.size = Pt(20)
    #  无序列表
    document.add_paragraph('第一点',style="List Bullet")
    document.add_paragraph('第二点',style="List Bullet")
    #  有序列表
    document.add_paragraph('第一点',style="List Number")
    document.add_paragraph('第二点',style="List Number")
    
    document.save('test.docx')  # 保存文档
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    添加图片

    from docx.shared import Cm
    document = Document()   
    pic = document.add_picture('picture1.jpg',width=Cm(10))  # 添加图片 
    document.paragraphs[-1].alignment = WD_ALIGN_PARAGRAPH.CENTER  # 将图片进行居中
    document.save('test.docx')  # 保存文档
    
    • 1
    • 2
    • 3
    • 4
    • 5

    添加表格

    table 有增加行 增加列 布局 自适应 等等操作 
    
    • 1

    add_column’,
    ‘add_row’,
    ‘alignment’,
    ‘autofit’,
    ‘cell’,
    ‘column_cells’,
    ‘columns’,
    ‘part’,
    ‘row_cells’,
    ‘rows’,
    ‘style’,
    ‘table’,
    ‘table_direction’

    document = Document()  
    table = document.add_table(rows = 2, cols= 2 )
    
    cell = table.cell(0,0)  # 读取0,0表格
    cell.text = 'allen'   # 修改表格文字 
    row = table.rows[1]  # 读取第2行 
    row.cells[0].text = 'row01'   # 修改表格文字 
    row.cells[1].text = 'row02'
    document.save('test.docx')  # 保存文档
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在表格中操作图片 实现图片的布局

    document = Document()  
    table = document.add_table(rows = 1, cols= 2, )
    table.alignment = WD_ALIGN_PARAGRAPH.CENTER # 表格居中对齐 
    cell = table.cell(0,0)  #  定位0,0 
    ph =cell.paragraphs[0]  # 定位0,0中第一个段落
    ph.alignment  = WD_ALIGN_PARAGRAPH.CENTER # 段落居中对齐 
    # 在段落中添加图片 
    run = ph.add_run()  
    run.add_picture('picture1.jpg', width=Cm(5))
    
    # 在另一个表格中加入图片 实现两个图片比列 
    cell = table.cell(0,1)
    ph =cell.paragraphs[0]
    run = ph.add_run()
    run.add_picture('picture2.jpg', width=Cm(5))
    ph.alignment  = WD_ALIGN_PARAGRAPH.CENTER
    document.save('test.docx')  # 保存文档
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    表格样式 合并 行高 列宽 文字垂直居中

    from docx.enum.table import WD_ALIGN_VERTICAL
    document = Document()  
    table = document.add_table(rows = 2, cols= 3, style='Table Grid')  # 设置框线
    table.rows[0].height=Cm(2)  # 设置行高 
    table.cell(0,0).merge(table.cell(0,1))  # 合并第一行第一个和第二个单元格表格 
    # 段落中文字居中
    table.cell(0,2).paragraphs[0].add_run('1')
    table.cell(0,2).paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER  
    table.cell(0,2).vertical_alignment = WD_ALIGN_VERTICAL.CENTER # 垂直居中
    table.cell(0,0).text = "0"
    table.cell(0,0).width = Cm(15)  # 设置列宽
    # table.cell(0,1).text = "1"
    document.save('test.docx')  # 保存文档
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    页眉页脚 分页

    document = Document()  
    # 页眉 
    header = document.sections[0].header 
    ph1 = header.add_paragraph('页眉')
    ph1.alignment = WD_ALIGN_PARAGRAPH.CENTER 
    # 页脚 
    footer = document.sections[0].footer
    footer.add_paragraph('页脚')
    # 分页 
    document.add_paragraph('段落1')
    document.add_page_break()
    document.add_heading('标题',level=2)
    document.save('test.docx')  # 保存文档
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    
    
    • 1
  • 相关阅读:
    第五十五天 P1036 [NOIP2002 普及组] 选数
    C++中执行shell命令,popen与system的区别
    TypeScript系列之类型 void
    python学习笔记(2)—— 控制流
    【数据库设计和SQL基础语法】--数据库设计基础--数据建模与ER图
    《算法通关村—轻松搞定合并二叉树》
    Ros2 学习02- ubuntu22.04 安装ros2
    《实战》提示框
    字符流,编码表,字符流写数据,字符流读数据
    JavaScript——WebAPI
  • 原文地址:https://blog.csdn.net/wwqnmdhmp/article/details/126621181