main.py 787 Bytes
Newer Older
Gabriel Couture's avatar
Gabriel Couture committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import subprocess

import docx2pdf
from mailmerge import MailMerge

FILEPATH = './tests/data/page_garde_publipostage.docx'

with MailMerge(FILEPATH) as document:
    fields = document.get_merge_fields()
    document.merge(
        num_copieur_clinique='1234',
        nom_md_famille_ou_ips='Couture',
        pr_nom_md_famille_ou_ips='Gabriel'
    )

    docx_filepath = './tests/data/page_garde.docx'
    output_dir = './tests/data'
    pdf_filepath = f'{output_dir}/page_garde.pdf'

    document.write(docx_filepath)

    try:
        docx2pdf.convert(docx_filepath)
    except NotImplementedError:  # Not implemented on linux
        process = subprocess.Popen(['libreoffice', '--headless', '--convert-to', 'pdf', docx_filepath, '--outdir', output_dir])
        process.communicate()