Convert PDF to .docx with Python
保存文档时,最好以'.docx'或'.pdf'格式,防止格式丢失。 #生活常识# #电脑#
This question shows research effort; it is useful and clear
7
Save this question.
Show activity on this post.
I'm trying to find a way to convert a PDF file to a .docx file with Python.
I have seen other posts related with this, but none of them seem to work correctly in my case.
I'm using specifically
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)
This gives me Output[1], but then, I can't find any .docx document in my folder.
I have LibreOffice 5.3 installed.
Any clues about it?
Thank you in advance!
Martin
1181 silver badge12 bronze badges
asked Apr 22, 2018 at 12:21
7
This answer is useful
9
Save this answer.
Show activity on this post.
I use this for multiple files
#### 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)
answered Feb 8, 2021 at 20:31
Comments
This answer is useful
4
Save this answer.
Show activity on this post.
I am not aware of a way to convert a pdf file into a Word file using libreoffice.
However, you can convert from a pdf to a html and then convert the html to a docx.
Firstly, get the commands running on the command line. (The following is on Linux. So you may have to fill in path names to the soffice binary and use a full path for the input file on your OS)
soffice --convert-to html ./my_pdf_file.pdf
then
soffice --convert-to docx:'MS Word 2007 XML' ./my_pdf_file.html
You should end up with:
my_pdf_file.pdf
my_pdf_file.html
my_pdf_file.docx
Now wrap the commands in your subprocess code
answered May 7, 2018 at 16:02
Comments
This answer is useful
4
Save this answer.
Show activity on this post.
Below code worked for me.
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()
answered Aug 4, 2020 at 21:12
Comments
This answer is useful
1
Save this answer.
Show activity on this post.
My approach does not follow the same methodology of using subsystems. However this one does the job of reading through all the pages of a PDF document and moving them to a docx file. Note: It only works with text; images and other objects are usually ignored.
#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.
answered Sep 6, 2020 at 11:03
Comments
This answer is useful
0
Save this answer.
Show activity on this post.
I have successfully done this with pdf2docx :
from pdf2docx import parse pdf_file = "test.pdf" word_file = "test.docx" parse(pdf_file, word_file, start=0, end=None)
answered Aug 9, 2021 at 23:40
1 Comment
It says that it's impossible to opend the file
2021-09-04T11:39:23.97Z+00:00
This answer is useful
-1
Save this answer.
Show activity on this post.
pip install spire.pdf pip install plum-dispatch==1.7.4 from spire.pdf.common import * from spire.pdf import * pdf = PdfDocument() pdf.LoadFromFile("input.pdf") pdf.SaveToFile("PdfToDocx.docx", FileFormat.DOCX) pdf.Close()
answered Jan 16, 2024 at 6:43
1 Comment
While this code my answer the question, your answer would benefit from having some explanation to what the code does to help future readers understand it
2024-01-16T11:02:10.833Z+00:00
Start asking to get answers
Find the answer to your question by asking.
Ask question
Explore related questions
See similar questions with these tags.
网址:Convert PDF to .docx with Python https://klqsh.com/news/view/344707
相关内容
Convert m to cmConvert cm to feet
Convert feet to cm
Convert NZD to Euro
Centimeters to feet and inches converter, Convert between cm and ft
Convert USD to EUR
Convert MYR to SEK
Euros to Indian rupees Exchange Rate. Convert EUR/INR
Euros to Australian dollars Exchange Rate. Convert EUR/AUD
Python Program to Print the Fibonacci sequence