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(跨平台,推荐)

基于libreoffice或unoconv的封装,支持跨平台(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. 格式保真 :
    • 复杂格式(如表格、图表)建议使用docx2pdf或Aspose.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")

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

相关推荐
张张123y15 分钟前
后端第十六课:Multi-Agent 多智能体(让多个 Agent 分工协作)
大数据·数据库·人工智能·python·deepseek·dsh·agent 框架
全栈练习生34 分钟前
大规模智能体仿真
python·ai
code2cat1 小时前
【随笔】vLLM前缀缓存:相同的开头怎样少算一次
人工智能·python·缓存·vllm
陈飞EXCEL1 小时前
北京杰飞 IT 旗下妙码生花少儿Python编程上线两大学习系统
开发语言·python·学习
Anesthesia丶2 小时前
基于 Qwen3-TTS 的多语言智能配音 Web 应用(Qwen3 TTS Studio)
python·ai·qwen-tts·agci
the局外人2 小时前
Python 中使用 SSE 实现大模型流式输出
后端·python·llm
桑年3 小时前
四、Pandas数据处理
人工智能·python·线性代数·pandas·数据可视化·数据处理
第九序言T3 小时前
Python 数据处理进阶(二十四)把脚本变成工具:argparse、logging 与定时任务
python·自动化·pandas
shelly聊外贸4 小时前
WhatsApp 聊天记录如何导出为 CSV?使用 Python 进行数据分析
开发语言·python·数据分析
I Am a robert girl4 小时前
从 FlowAct-R2 拆解“会说话的数字人“:流式多模态参考与主动式 Agent 规划
python·数字人·多模态大模型·扩散transformer·流式生成·agent规划