[Python] 纯文本查看 复制代码 import os
import pdf2docx
def convert_pdf_to_doc():
pdf_path = input("请输入要转换的PDF文件路径:")
# 获取PDF文件所在的目录和文件名
pdf_dir = os.path.dirname(pdf_path)
pdf_filename = os.path.basename(pdf_path)
# 将文件扩展名改为.docx
doc_filename = os.path.splitext(pdf_filename)[0] + ".docx"
# 合成输出的DOC文件路径
doc_path = os.path.join(pdf_dir, doc_filename)
pdf_file = open(pdf_path, 'rb')
doc_file = open(doc_path, 'w')
pdf2docx.convert(pdf_file, doc_file, start=0, end=None)
pdf_file.close()
doc_file.close()
print("转换完成,已保存为:" + doc_path)
# 运行脚本
convert_pdf_to_doc()
|