6行代码!用Python将PDF转为word

pdf转word应该算是一个很常见的需求了
网上有些免费的转换工具,一方面不安全,有文件泄露风险,另一方面有免费转换的次数限制。
今天向大家分享一个很好用的工具:pdf2docx

安装

$ pip install pdf2docx

用法也很简单,核心方法是Converter
我写了一个小脚本,如有需要,大家可以直接copy走。

# -*- coding: utf-8 -*-
"""
Created on Sat Aug  7 16:36:59 2021

@author: LaoHu
"""
import argparse
from pdf2docx import Converter

def main(pdf_file,docx_file):
    cv = Converter(pdf_file)
    cv.convert(docx_file, start=0, end=None)
    cv.close()
    
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--pdf_file",type=str)
    parser.add_argument('--docx_file',type=str)
    args = parser.parse_args()
    main(args.pdf_file,args.docx_file)

用法

python pdf2word.py --pdf_file  pdf文件路径example.pdf --docx_file 输出word文件的路径example.docx

不喜欢命令行跑脚本的同学可以copy下面简化版

from pdf2docx import Converter
pdf_file = 'pdf文件路径'
docx_file = '输出word文件的路径'
cv = Converter(pdf_file)
cv.convert(docx_file, start=0, end=None)
cv.close()
原文地址:https://www.cnblogs.com/jpld/p/15122989.html