使用Python实现一键转换pdf为docx word

使用前先安装pdf2docx模块

pip install pdf2docx

然后创建Python脚本,内容为:

from pdf2docx import Converter
import os

#用法:只需要输入文件路径,就可以自动转换为docx文件,并保存在当前路径下

def pdf_to_word(pdf_file):
    # 自动生成Word文件路径
    word_file = os.path.splitext(pdf_file)[0] + '.docx'

    # 创建一个Converter对象
    cv = Converter(pdf_file)

    # 将PDF转换为Word文档
    cv.convert(word_file, start=0, end=None)

    # 关闭Converter对象
    cv.close()

    print(f"PDF文件'{pdf_file}'已成功转换为Word文件'{word_file}'")


# 获取用户输入的PDF文件路径
pdf_file = input('请输入PDF文件路径(例如:C:\\Users\\ccieluo\\CCIESecurity.pdf):')

# 调用转换函数
pdf_to_word(pdf_file)

然后运行这个脚本,只需要输入pdf路径,就会自动把pdf转换为当前路径下的docx文件

你可能感兴趣的:(Python,python,pdf,开发语言)