5

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
  • 56
  • 1
  • 11
Also
  • 101
  • 1
  • 2
  • 6
  • Try `lowriter --invisible --convert-to doc "/my/pdf/folder/filename.pdf"` in a terminal window. – Stop harming Monica Apr 22 '18 at 12:44
  • @Goyo thanks. But I'm not able to run this commando in the terminal. It doesn't recognize lowriter as an executable command. Why is that? – Also Apr 22 '18 at 13:17
  • 3
    How could I know? It is probably related to the way you installed libreoffice. But you'd better figure it out, you can't expect python to run a program when you are unable to run it yourself. – Stop harming Monica Apr 22 '18 at 14:20
  • What is your operating system? Use [my answer from two weeks ago](https://stackoverflow.com/a/49763069/5100564) but modify the paths. – Jim K Apr 23 '18 at 18:59
  • @JimK Thank you very much. I saw your post recently, and I used it as a help for my trouble, and it worked perfectly for converting the pdf to odg. (I checked your answer for that reason). However, I was looking for the conversion to .docx, that seems to be more difficult... In any case, my operating system is Windows 7. – Also Apr 24 '18 at 08:55
  • @Also - did you find a result for this thread? I am trying to do the same - convert pdf to docx and .txt. – user1261774 Jul 02 '18 at 00:37
  • Can somebody help me on this. ? i need to convert the same ie, from pdf to docx and it should have more accuracy level.. – ideano1 Sep 21 '18 at 12:09

5 Answers5

6

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)

simon
  • 61
  • 1
  • 1
3

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

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
2

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()
Omkar
  • 3,253
  • 3
  • 20
  • 36
1

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. 
SaaSy Monster
  • 552
  • 2
  • 16
0

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)
Kush254
  • 29
  • 3