在python中将pdf转换为docx格式

时间:2019-09-03 20:58:58

标签: pdf docx python-docx pdfminer

请如何将pdf转换为docx。我尝试使用pdfminer转换为html来提取文本,但效果仍然不够好。

2 个答案:

答案 0 :(得分:0)

您可以使用这样的在线工具:https://pdf2docx.com

或者如果您在Linux上,请使用libreoffice api:

libreoffice --headless --convert-to=pdf example.docx

答案 1 :(得分:0)

pdf2docx

  1. 安装 pdf2docx 包点击 here

安装

  • 克隆或下载 pdf2docx

     pip install pdf2docx
         or
     # download the package and install your environment
     python setup.py install 
    
  • 选项 1

    from pdf2docx import Converter
    
    pdf_file  = r'C:\Users\ABCD\Desktop\XYZ/Document1.pdf'# source file 
    docx_file = r'C:\Users\ABCD\Desktop\XYZ/sample.docx'  # destination file
    
    # convert pdf to docx
    cv = Converter(pdf_file)
    cv.convert(docx_file, start=0, end=None)
    cv.close()
    
    #Output
    
    Parsing Page 53: 53/53...
    Creating Page 53: 53/53...
    --------------------------------------------------
    Terminated in 6.258919400000195s.
    
  • 选项 2

    from pdf2docx import parse
    
    pdf_file  = r'C:\Users\ABCD\Desktop\XYZ/Document2.pdf' # source file
    docx_file = r'C:\Users\ABCD\Desktop\XYZ/sample_2.docx' # destination file
    
    # convert pdf to docx
    parse(pdf_file, docx_file, start=0, end=None)
    
    # output
    Parsing Page 53: 53/53...
    Creating Page 53: 53/53...
    --------------------------------------------------
    Terminated in 5.883666100000482s.