【问题标题】:Convert PDF to .docx with Python使用 Python 将 PDF 转换为 .docx
【发布时间】:2018-10-02 14:21:20
【问题描述】:

我正在努力寻找使用 Python 将 PDF 文件转换为 .docx 文件的方法。

我看过与此相关的其他帖子,但在我的情况下,它们似乎都不能正常工作。

我是专门用的

import os
import subprocess

for top, dirs, files in os.walk('/my/pdf/folder'):
    for filename in files:
        if filename.endswith('.pdf'):
            abspath = os.path.join(top, filename)
            subprocess.call('lowriter --invisible --convert-to doc "{}"'
                            .format(abspath), shell=True)

这给了我输出 [1],但是我在我的文件夹中找不到任何 .docx 文档。

我安装了 LibreOffice 5.3。

有什么线索吗?

提前谢谢你!

【问题讨论】:

  • 在终端窗口中尝试lowriter --invisible --convert-to doc "/my/pdf/folder/filename.pdf"
  • @Goyo 谢谢。但我无法在终端中运行这个突击队。它不会将 lowriter 识别为可执行命令。这是为什么呢?
  • 我怎么知道?这可能与您安装 libreoffice 的方式有关。但是你最好想清楚,你不能自己运行程序的时候,不能指望python来运行它。
  • 你的操作系统是什么?使用my answer from two weeks ago,但修改路径。
  • @JimK 非常感谢。我最近看到了你的帖子,我用它来解决我的麻烦,它非常适合将 pdf 转换为 odg。 (出于这个原因,我检查了您的答案)。但是,我一直在寻找转换为 .docx 的方法,这似乎更困难......无论如何,我的操作系统是 Windows 7。

标签: python pdf docx libreoffice doc


【解决方案1】:

我不知道使用 libreoffice 将 pdf 文件转换为 Word 文件的方法。
但是,您可以从pdf 转换为html,然后将html 转换为docx
首先,获取在命令行上运行的命令。 (以下是在 Linux 上。因此您可能需要填写 soffice 二进制文件的路径名,并为您的操作系统上的输入文件使用完整路径)

soffice --convert-to html ./my_pdf_file.pdf

然后

soffice --convert-to docx:'MS Word 2007 XML' ./my_pdf_file.html

你应该得到:

my_pdf_file.pdf
my_pdf_file.html
my_pdf_file.docx

现在将命令包装在您的 subprocess 代码中

【讨论】:

    【解决方案2】:

    我的方法与使用子系统的方法不同。但是,这个功能可以阅读 PDF 文档的所有页面并将它们移动到 docx 文件中。注意:它仅适用于文本;图像和其他对象通常会被忽略。

    #Description: This python script will allow you to fetch text information from a pdf file
    
    #import libraries
    
    import PyPDF2
    import os
    import docx
    
    mydoc = docx.Document() # document type
    pdfFileObj = open('pdf/filename.pdf', 'rb') # pdffile loction
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj) # define pdf reader object
    
    # Loop through all the pages
    
    for pageNum in range(1, pdfReader.numPages):
            pageObj = pdfReader.getPage(pageNum)
            pdfContent = pageObj.extractText()  #extracts the content from the page. 
            print(pdfContent) # print statement to test output in the terminal. codeline optional.
            mydoc.add_paragraph(pdfContent) # this adds the content to the word document
            
    mydoc.save("pdf/filename.docx") # Give a name to your output file. 
    

    【讨论】:

      【解决方案3】:

      我将它用于多个文件

      ####
      from pdf2docx import Converter
      import os
      
      # # # dir_path for input reading and output files & a for loop # # #
      
      path_input = '/pdftodocx/input/'
      path_output = '/pdftodocx/output/'
      
      for file in os.listdir(path_input):
          cv = Converter(path_input+file)
          cv.convert(path_output+file+'.docx', start=0, end=None)
          cv.close()
          print(file)
      
      

      【讨论】:

        【解决方案4】:

        我已经用 pdf2docx 成功地做到了:

        from pdf2docx import parse
        pdf_file = "test.pdf"
        word_file = "test.docx"
        parse(pdf_file, word_file, start=0, end=None)
        

        【讨论】:

        • 表示无法打开文件
        【解决方案5】:

        以下代码对我有用。

        import win32com.client
        word = win32com.client.Dispatch("Word.Application")
        word.visible = 1
        pdfdoc = 'NewDoc.pdf'
        todocx = 'NewDoc.docx'
        wb1 = word.Documents.Open(pdfdoc)
        wb1.SaveAs(todocx, FileFormat=16)  # file format for docx
        wb1.Close()
        word.Quit()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-14
          • 2017-08-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-19
          • 2014-06-28
          • 1970-01-01
          相关资源
          最近更新 更多