将PDF转换为DOC(Python/Bash)

Alv*_*oAV 9 python pdf bash doc docx

我看过一些允许用户上传PDF并返回DOC文件的页面,比如PdfToWord

有没有办法使用Python或任何Unix命令将PDF文件转换为文件DOC/DOCX

提前致谢

ham*_*ich 7

这很困难,因为PDF是面向表示的,而word文档是面向内容的.我测试了两个,可以推荐以下项目.

  1. PyPDF2
  2. PDFMiner

但是,你肯定会失去转换中的表现方面.


小智 7

如果您安装了LibreOffice

lowriter --invisible --convert-to doc '/your/file.pdf'
Run Code Online (Sandbox Code Playgroud)

如果你想使用Python:

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)
Run Code Online (Sandbox Code Playgroud)

  • @alvaroav plz分享你的命令,即使用libreoffice将pdf转换为word.我有最新版的libreoffice (3认同)
  • @ user3058846我试过了,但是什么也没做 (2认同)
  • 对于 macOS 用户:`/Applications/LibreOffice.app/Contents/MacOS/soffice --invisible --infilter="writer_pdf_import" --convert-to docx:"MS Word 2007 XML" file-to-convert.pdf` (2认同)

小智 7

如果你想转换 PDF -> MS Word 类型的文件,比如 docx,我遇到了这个.

阿辛·沙比尔写道:

import glob
import win32com.client
import os

word = win32com.client.Dispatch("Word.Application")
word.visible = 0

pdfs_path = "" # folder where the .pdf files are stored
for i, doc in enumerate(glob.iglob(pdfs_path+"*.pdf")):
    print(doc)
    filename = doc.split('\\')[-1]
    in_file = os.path.abspath(doc)
    print(in_file)
    wb = word.Documents.Open(in_file)
    out_file = os.path.abspath(reqs_path +filename[0:-4]+ ".docx".format(i))
    print("outfile\n",out_file)
    wb.SaveAs2(out_file, FileFormat=16) # file format for docx
    print("success...")
    wb.Close()

word.Quit()
Run Code Online (Sandbox Code Playgroud)

这对我来说就像一个魅力,转换了 500 页带有格式和图像的 PDF。

  • 我认为设置 `word.visible = 1` 是更好的选择。这将允许用户看到所有以文字显示的消息或警告。如果我们设置“word.visible = 0”,word将无法显示任何错误/警告,从而使调试体验变得复杂。 (2认同)
  • @eleks007-san,reqs_path 未定义 (2认同)