Skip to content

Instantly share code, notes, and snippets.

@webstyle
Forked from MichalZalecki/docx2pdf.py
Created December 19, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webstyle/3c1e1b464f32139a65fa16b2f745162e to your computer and use it in GitHub Desktop.
Save webstyle/3c1e1b464f32139a65fa16b2f745162e to your computer and use it in GitHub Desktop.
Converting DOCX to PDF using Python
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())
if filename is None:
raise LibreOfficeError(process.stdout.decode())
else:
return filename.group(1)
def libreoffice_exec():
# TODO: Provide support for more platforms
if sys.platform == 'darwin':
return '/Applications/LibreOffice.app/Contents/MacOS/soffice'
return 'libreoffice'
class LibreOfficeError(Exception):
def __init__(self, output):
self.output = output
if __name__ == '__main__':
print('Converted to ' + convert_to(sys.argv[1], sys.argv[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment