Skip to content

Instantly share code, notes, and snippets.

@ego008
Forked from MichalZalecki/docx2pdf.py
Created August 17, 2017 02:25
Show Gist options
  • Select an option

  • Save ego008/fe4ea8943723fe7dd9902424b10de12e to your computer and use it in GitHub Desktop.

Select an option

Save ego008/fe4ea8943723fe7dd9902424b10de12e to your computer and use it in GitHub Desktop.

Revisions

  1. @MichalZalecki MichalZalecki created this gist Jul 8, 2017.
    31 changes: 31 additions & 0 deletions docx2pdf.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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]))