Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法使用python将Pdf文件转换为Docx_Python - Fatal编程技术网

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

有没有办法使用python将Pdf文件转换为Docx,python,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.num

我想知道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())

如果你有很多页面的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())

如果你碰巧有微软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()

如果你碰巧有微软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()

谢谢我已经尝试了那个链接,但它不能满足我的要求。谢谢。我尝试过那个链接,但它不能满足我的要求。是的,它会的。我对此做了广泛的研究。…在Python中转换是不可能的。下一个最好的解决方案是你不感兴趣的在线工具。是的,它会的。我对此做了广泛的研究。…你不可能得到,因为它是Python中的转换。下一个最好的解决方案是您不感兴趣的在线工具。@Ahsin san,reqs_路径未定义。@Ahsin san,reqs_路径未定义。