使用 Python 处理 Word 和 PDF

使用 Python 处理 Word 等办公自动化的文档用的库都比较固定,因此这篇文章只是帮自己快速回忆,更多内容还是查询官方文档

处理 Word 文档的基本步骤

  1. 新建 Word 文档
    document = Document()
    
  2. 设置默认格式(增加对中文字体的支持)
    document.styles['Normal'].font.name = u'字体名'
    document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'字体名')
    
  3. 新建自然段
    p1 = document.add_paragraph()
    
  4. 设置自然段格式(增加对中文字体的支持)
    run1.font.name = '字体名'
    run1._element.rPr.rFonts.set(qn('w:eastAsia'), u'字体名')
    run1.font.size = Pt(字号)
    
  5. 插入分页符
    document.add_page_break()
    
  6. 保存文档
    document.save('保存地址')
    

在 Word 中添加表格和图片

  1. 创建表格
    table = document.add_table(rows=3, cols=3, style='Table Grid')
    
  2. 合并单元格
    table.cell(0, 0).merge(table.cell(0, 2))
    
  3. 在单元格中写入内容
    table_run1 = table.cell(0 ,0).paragraphs[0].add_run('表格标题')
    table.cell(1 ,0).text = '想要输入的文本'
    table.cell(1 ,1).text = '想要输入的文本'
    table.cell(1 ,2).text = '想要输入的文本'
    
  4. 在单元格中插入图片
    document.add_picuture('图片名称', width=Inches(图片大小))
    
  5. 其他
    Word 中表格的其他操作,如设置字体,对齐方式等均可参照 Word 文本段落操作方式。

快速将 Word 转换为 PDF

这里主要使用了 Python 中的 Win32com 的库,其中启用 Office 组件使用了其注册表地址。代码示例:
from win32com.client import Dispatch, constants, gencache

docx_path = 'Docx 文件路径'
pdf_path = 'PDF 文件路径'

gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4)

wd = Dispatch("Word.Application")

doc = wd.Documents.Open(docx_path, ReadOnly=1)
doc.ExportAsFixedFormat(pdf_path, constants.wdExportFormatPDF, Item=constants.wdExportDocumentWithMarkup,
                        CreateBookmarks=constants.wdExportCreateHeadingBookmarks)

wd.Quit(constants.wdDoNotSaveChanges)

参考资料

评论