python将pdf转为docx

如何使用python实现将pdf文件转为docx文件

1.首先要安装pdf2docx库

python 复制代码
pip install pdf2docx

2.实现转换

python 复制代码
from pdf2docx import Converter


def convert_pdf_to_docx(input_pdf, output_docx):
    # 创建一个PDF转换器对象
    pdf_converter = Converter(input_pdf)

    # 将PDF转换为docx文件
    pdf_converter.convert(output_docx)

    # 关闭转换器以释放资源
    pdf_converter.close()


if __name__ == "__main__":
    input_pdf = "C:/Users/Windows/Desktop/test.pdf"  # 替换为自己实际的PDF文件路径
    output_docx = "C:/Users/Windows/Desktop/python.docx"  # 替换为自己实际的docx输出文件路径

    # 调用函数进行转换
    convert_pdf_to_docx(input_pdf, output_docx)
    print("PDF文件已成功转换为Word格式.")

执行成功:

之后就可以在自己输出路径下看到相应的docx文件了。

相关推荐
dev派20 分钟前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风2 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽12 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户83562907805116 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon18 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly18 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程18 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly20 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风1 天前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风1 天前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python