craver2000
Posts: 7
Joined: Mon Dec 11, 2017 1:43 am

Converting docx to pdf, with python script

Sun Feb 18, 2018 5:56 am

I would like to convert a docx file to pdf on my raspberry pi (stretch OS)

What is the best way to do so using a python script on raspberry pi? I think libreoffice have functions like this, but I would like to automate the conversion, so the docx to pdf conversion is run on a python script. Are there python modules that allows me to do this? I appreciate any advice on this, thanks!

User avatar
rpdom
Posts: 12936
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: Converting docx to pdf, with python script

Sun Feb 18, 2018 6:11 am

Libreoffice can do this in batch mode

Code: Select all

libroffice --convert-to pdf myfile.docx
will create myfile.pdf

craver2000
Posts: 7
Joined: Mon Dec 11, 2017 1:43 am

Re: Converting docx to pdf, with python script

Sun Feb 18, 2018 6:57 am

Thanks. As suggested, I've written this into my script:

Code: Select all

def conversiontopdf():
	print("starting conversion to pdf")
	libroffice --convert-to pdf demo.docx
However, I get this error on terminal when I run the script.

Code: Select all

  File "script.py", line 107
    libroffice --convert-to pdf demo.docx
                              ^
SyntaxError: invalid syntax
Any ideas why there would be a syntax error?

ghans
Posts: 7817
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Converting docx to pdf, with python script

Sun Feb 18, 2018 7:56 am

You can't simply write commands into a Python script, especially if they are external
programs. This is a fundamental difference between bash scripts (which must support this) and other languages.

You need to use subprocess.

Code: Select all

import subprocess
output = subprocess.check_output(['libreoffice', '--convert-to', 'pdf' ,'demo.docx'])
print output
ghans
Last edited by ghans on Sun Feb 18, 2018 10:16 am, edited 2 times in total.
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

craver2000
Posts: 7
Joined: Mon Dec 11, 2017 1:43 am

Re: Converting docx to pdf, with python script

Sun Feb 18, 2018 8:28 am

Thanks ghans. I've implemented your code into my script.

Code: Select all

def conversiontopdf():
	print("starting conversion to pdf")
	import subprocess
	output = subprocess.check_output('libreoffice', '--convert-to', 'pdf' ,'demo.docx')
	print output
I ran it and got:

Code: Select all

  File "script.py", line 108, in conversiontopdf
    output = subprocess.check_output('libreoffice', '--convert-to', 'pdf' ,'demo.docx')
  File "/usr/lib/python2.7/subprocess.py", line 212, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 339, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
What does this bufsize error mean and how can I resolve it?

ghans
Posts: 7817
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Converting docx to pdf, with python script

Sun Feb 18, 2018 8:34 am

Sorry, forgot the square brackets. Check my edited post.
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

craver2000
Posts: 7
Joined: Mon Dec 11, 2017 1:43 am

Re: Converting docx to pdf, with python script

Sun Feb 18, 2018 8:47 am

Thank you, it works now!

User avatar
rpdom
Posts: 12936
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: Converting docx to pdf, with python script

Sun Feb 18, 2018 10:38 am

rpdom wrote:
Sun Feb 18, 2018 6:11 am
Libreoffice can do this in batch mode

Code: Select all

libreoffice --convert-to pdf myfile.docx
will create myfile.pdf
Edit: I should have spelt libreoffice correctly. Fixed now.

shanmukh
Posts: 1
Joined: Tue May 22, 2018 6:44 am

Re: Converting docx to pdf, with python script

Tue May 22, 2018 6:47 am

I have used the following code it return's error.
I have libreoffice 6.0 installed in my fedora 24 os

profileId="somename"

def conversiontopdf(name):
print("starting conversion to pdf")

output = subprocess.check_output(['libreoffice', '--convert-to', 'pdf' ,name])
print output
conversiontopdf(profileId+'docx')



error is:
starting conversion to pdf
Error: source file could not be loaded

rotwang
Posts: 240
Joined: Sat Dec 07, 2013 1:12 pm

Re: Converting docx to pdf, with python script

Tue May 22, 2018 10:43 am

shanmukh wrote:
Tue May 22, 2018 6:47 am
I have used the following code it return's error.
I have libreoffice 6.0 installed in my fedora 24 os

profileId="somename"

def conversiontopdf(name):
print("starting conversion to pdf")

output = subprocess.check_output(['libreoffice', '--convert-to', 'pdf' ,name])
print output
conversiontopdf(profileId+'docx')



error is:
starting conversion to pdf
Error: source file could not be loaded
First, always enclose code, especially python, within <code> </code> tags. If you don't the indentation is lost, and as this is the only guide to structure for python, the result is text which is meaningless.
Second, if the filename you want to operate on is "somename.docx" then you will have to supply the "." somewhere, I'd suggest you change
conversiontopdf(profileid+'docx') to conversiontopdf)profileid+'.docx')
also add the name to the "print("starting conversion to pdf")"

Roger

Return to “Advanced users”