繁体   English   中英

有没有办法使用python将Pdf文件转换为Docx

[英]Is there any way to convert Pdf file to Docx using python

我想知道 python(工具或函数等)是否有办法将我的 pdf 文件转换为 doc 或 docx?

我知道在线转换器,但我需要在 Python 代码中使用它。

如果你有很多页面的pdf..下面的代码将起作用:

import PyPDF2

    path="C:\\ .... "
    text=""
    pdf_file = open(path, 'rb')
    text =""
    read_pdf = PyPDF2.PdfFileReader(pdf_file)
    c = read_pdf.numPages
    for i in range(c):
         page = read_pdf.getPage(i)
         text+=(page.extractText())

如果您碰巧有 MS Word,那么使用 COM 有一种非常简单的方法可以做到这一点。 这是我编写的脚本,可以通过调用 Word 应用程序将 pdf 转换为 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM