python如何word转pdf

在Python中,将Word文档(.docx.doc)转换为PDF可以通过多种库实现。以下是几种常见的方法及详细步骤:


方法1:使用 python-docx + comtypes(仅Windows,需安装Word)

适用于Windows系统,依赖本地安装的Microsoft Word。

python 复制代码
import comtypes.client

def docx_to_pdf(input_path, output_path):
    word = comtypes.client.CreateObject("Word.Application")
    doc = word.Documents.Open(input_path)
    doc.SaveAs(output_path, FileFormat=17)  # 17是PDF格式的代码
    doc.Close()
    word.Quit()

# 示例
docx_to_pdf("input.docx", "output.pdf")

注意

  • 仅支持Windows且需安装Microsoft Word。
  • 需安装comtypes库:pip install comtypes

方法2:使用 docx2pdf(跨平台,推荐)

基于libreofficeunoconv的封装,支持跨平台(Windows/macOS/Linux)。

python 复制代码
from docx2pdf import convert

# 单文件转换
convert("input.docx", "output.pdf")

# 批量转换整个文件夹
convert("input_folder/", "output_folder/")

安装

bash 复制代码
pip install docx2pdf

依赖

  • Windows:需安装Microsoft Word或LibreOffice。
  • macOS/Linux:需安装LibreOffice(sudo apt install libreoffice)。

方法3:使用 pywin32(仅Windows,类似comtypes

comtypes类似,但使用pywin32库。

python 复制代码
import win32com.client

def docx_to_pdf(input_path, output_path):
    word = win32com.client.Dispatch("Word.Application")
    doc = word.Documents.Open(input_path)
    doc.SaveAs(output_path, FileFormat=17)
    doc.Close()
    word.Quit()

# 示例
docx_to_pdf("input.docx", "output.pdf")

安装

bash 复制代码
pip install pywin32

方法4:使用 unoconv(Linux优先)

依赖LibreOffice的命令行工具unoconv

python 复制代码
import subprocess

def docx_to_pdf(input_path, output_path):
    subprocess.run(["unoconv", "-f", "pdf", "-o", output_path, input_path])

# 示例
docx_to_pdf("input.docx", "output.pdf")

安装

bash 复制代码
# Linux
sudo apt install unoconv
# macOS
brew install unoconv

方法5:使用 Aspose.Words(付费库,功能强大)

适用于企业级应用,支持高级格式转换。

python 复制代码
import asposewords as aw

doc = aw.Document("input.docx")
doc.save("output.pdf", aw.SaveFormat.PDF)

安装

bash 复制代码
pip install aspose-words

注意事项

  1. 跨平台兼容性
    • 推荐docx2pdf(需LibreOffice)或python-docx+comtypes(仅Windows)。
  2. 格式保真
    • 复杂格式(如表格、图表)建议使用docx2pdfAspose.Words
  3. 无头模式
    • Linux服务器可配置LibreOffice的无头模式:

      bash 复制代码
      libreoffice --headless --convert-to pdf *.docx

完整示例(推荐docx2pdf

python 复制代码
from docx2pdf import convert

def convert_word_to_pdf(input_file, output_file):
    try:
        convert(input_file, output_file)
        print(f"转换成功:{output_file}")
    except Exception as e:
        print(f"转换失败:{e}")

# 使用示例
convert_word_to_pdf("report.docx", "report.pdf")

选择方法时,请根据操作系统、依赖环境和需求(如批量转换、格式复杂度)决定。

相关推荐
IVEN_14 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang15 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮15 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling15 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮19 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽19 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽2 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers