[英]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.