Reputation: 213
It doesn't create a pdf file in the directory also it prints - None
import sys
import subprocess
import re
def convert_to(folder, source, timeout=None):
args = [libreoffice_exec(), '--headless', '--convert-to', 'pdf', '--outdir', folder, source]
process = subprocess.run(args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, timeout=timeout)
filename = re.search('-> (.*?) using filter', process.stdout.decode())
return filename
def libreoffice_exec():
# TODO: Provide support for more platforms
if sys.platform == 'darwin':
return '/Applications/LibreOffice.app/Contents/MacOS/soffice'
return 'libreoffice'
result = convert_to('/directory_to_save_file', '/File_path', timeout=15)
print(result)
Could anyone give me solution where I can easily convert docx to pdf? Thanks in advance!
Upvotes: 3
Views: 7275
Reputation: 1960
Aspose.Words for Python provides an ability to convert DOCX and other document formats to PDF. But note, Aspose.Words is a commercial product. Code is simple - load a document and save it as PDF:
import aspose.words as aw
doc = aw.Document("in.docx")
doc.save("out.pdf")
Additional conversions options can be specified using PdfSaveOptions
, for example PDF compliance:
https://docs.aspose.com/words/python-net/convert-a-document-to-pdf/
import aspose.words as aw
doc = aw.Document("C:\\Temp\\in.docx")
opt = aw.saving.PdfSaveOptions()
opt.compliance = aw.saving.PdfCompliance.PDF_UA1
doc.save("C:\\Temp\\out.pdf", opt)
In addition, when you convert document to PDF, to get an accurate result, the fonts used in the original document should be available in the environment where conversion is performed. Otherwise the fonts are substituted with available ones, that might lead to layout differences due to different font metrics. https://docs.aspose.com/words/python-net/manipulating-and-substitution-truetype-fonts/
Upvotes: 0
Reputation: 76
You can try below code in your function
import subprocess
output = subprocess.check_output(['libreoffice', '--convert-to', 'pdf' ,'demo.docx'])
print output
Upvotes: 6