1. 基本介绍
python-docx是用于创建和修改 Microsoft Word(.docx)文件的 Python 第三方库。通过它,你能够以编程方式生成 Word 文档,添加文本、表格、图像、样式等元素
1 2 3 4 5 6 7
   | from docx import Document
  doc = Document()
  doc = Document(path)
  doc.save(path)
 
  | 
 
word中常见的操作:添加标题(add_heading),添加正文(add_paragraph),添加表格(add_table)
2. 文本块Run
文本块Run对象是通过add_run()返回的对象,如下:
1 2 3 4 5 6 7 8 9 10
   | head = doc.add_heading()
  run = head.add_run()
 
  table = doc.add_table(rows=2, cols=2,style='Table Grid')
  cell = table.cell(0, 0)
  run = cell.paragraphs[0].add_run()
 
  | 
 
通过Run对象,我们可以对文本进行多种定制化操作,例如字体样式,字体大小/颜色,对齐方式等等,每使用一次add_run()就在原先模块的基础上追加了新的文本
字体样式
确保文本是add_run添加的
1 2 3 4 5
   | from docx.oxml.ns import qn
  run.font.name = '黑体'
  run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') 
 
  | 
 
字体大小
1 2 3
   | from docx.shared import Pt
  run.font.size = Pt(12)  
 
  | 
 
字体颜色
1 2 3
   | from docx.shared import RGBColor
  run.font.color.rgb = RGBColor(255, 0, 0)  
 
  | 
 
粗体、斜体、下划线
1 2 3 4 5 6
   |  run.bold = True     
  run.italic = True     
  run.underline = True   
 
  | 
 
2. 标题相关操作
1 2
   |  heading = doc.add_heading(text,level=1)
 
  | 
 
标题对齐
1 2 3
   | from docx.enum.text import WD_ALIGN_PARAGRAPH
  heading.alignment = WD_ALIGN_PARAGRAPH.CENTER  
 
  | 
 
标题前后间距
借助段落相关API实现
1 2 3 4
   |  heading.paragraph_format.space_before = Pt(12)  
  heading.paragraph_format.space_after = Pt(6)   
 
  | 
 
3. 段落相关操作
1 2
   |  p = doc.add_paragraph()
 
  | 
 
段落对齐方式
1 2 3
   |  from docx.enum.text import WD_ALIGN_PARAGRAPH p.alignment = WD_ALIGN_PARAGRAPH.CENTER  
 
  | 
 
缩进设置
1 2 3 4 5
   | from docx.shared import Cm
  p.paragraph_format.left_indent = Cm(1.0)  
  p.paragraph_format.first_line_indent = Cm(0.5)  
 
  | 
 
间距
1 2 3 4 5 6 7 8 9 10 11
   |  p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE 
  p.line_spacing_rule = WD_LINE_SPACING.EXACTLY  p.paragraph_format.line_spacing = Pt(21)
  p.paragraph_format.first_line_indent = Pt(42)
  p.paragraph_format.space_before = Pt(6)  
  p.paragraph_format.space_after = Pt(6)   
 
  | 
 
分页
1 2
   |  p.paragraph_format.page_break_before = True  
 
  | 
 
4. 表格相关操作
1 2
   |  table = doc.add_table(rows=3, cols=3, style='Table Grid')
 
  | 
 
表格尺寸
1 2 3 4
   |  rows_count = len(table.rows)  
  cols_count = len(table.columns)  
 
  | 
 
添加新行
1 2
   |  new_row = table.add_row().cells
 
  | 
 
访问单元格cell
1 2 3 4 5 6
   |  cell = table.cell(0, 1)  
  cell = table.columns[1].cells[0] 
  cell = table.rows[0].cells[1]
 
  | 
 
合并单元格
1 2 3 4
   |  table.cell(0, 0).merge(table.cell(0, 1))
  table.cell(0, 0).merge(table.cell(1, 1))  
 
  | 
 
单元格对齐
1 2 3 4 5
   | from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_ALIGN_VERTICAL
  cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER 
  cell.vertical_alignment = WD_ALIGN_VERTICAL.CENTER  
 
  | 
 
宽度与高度
高度通常是按行为单位
宽度
1 2 3 4 5 6
   |  table.width = Cm(15)  
  table.columns[0].width = Cm(3)  
  table.cell(0, 0).width = Cm(5)  
 
  | 
 
高度
1 2 3 4 5 6 7 8
   | from docx.enum.text import WD_ROW_HEIGHT_RULE
  table.rows[0].height_rule = WD_ROW_HEIGHT_RULE.EXACTLY table.rows[0].height = Cm(1)
  table.rows[0].height = None 
  table.rows[0].height_rule = WD_ROW_HEIGHT_RULE.AUTO
 
  | 
 
单元格内容
1 2 3 4 5 6 7 8
   |  cell.text = '新内容'  
 
  cell.paragraphs[0].text = "保留格式的文本" 
 
  cell.paragraphs[0].runs[0].text = "第一个段落的第一个run"
 
  | 
 
5. 图片相关操作
通过段落加Run的方式添加图片,图片大小需要在添加时指定
1 2 3 4 5 6
   |  run = cell.paragraphs[0].add_run()
  run.add_picture("./test/1.jpg", width=Cm(6.4))
  cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
 
  | 
 
6 节Section
每个Word文档至少有一个节。如果文档没有手动分节,整个文档就是一个节,通过插入分节符(如 “下一页”、“连续”、“偶数页” 等),可以将文档分成多个节,各节可以有独立的页面设置。
页面设置相关属性
1 2 3 4 5 6 7 8 9 10 11 12 13
   | from docx.enum.section import WD_ORIENT from docx.shared import Cm section = doc.sections[0]
  section.orientation = WD_ORIENT.LANDSCAPE
  section.page_width = Cm(29.7) section.page_height = Cm(21.0)
  section.left_margin = Cm(2.5)     section.right_margin = Cm(2.5)    section.top_margin = Cm(3)      section.bottom_margin = Cm(3)  
 
  | 
 
分节符
1 2 3 4 5
   | from docx.enum.section import WD_SECTION
  doc.add_section(WD_SECTION.NEW_PAGE)
  doc.add_section(WD_SECTION.CONTINUOUS)
 
  |