Created
July 8, 2017 08:09
-
-
Save MichalZalecki/92fd007699004ae7d806274d3a0d5476 to your computer and use it in GitHub Desktop.
Converting DOCX to PDF using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
How can I use this code on windows or linux machine?
do I need to install libreoffice module on my local?
Full setup Flask and Docker 🚢 Converting DOCX to PDF using Python
Thanks a lot!!
Full setup Flask and Docker 🚢 Converting DOCX to PDF using Python
Thanks, i was on wsl , this work as well. i just integrate it on my django project
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full setup Flask and Docker 🚢 Converting DOCX to PDF using Python