[Python] apply a template before creating a pdf file in libreoffice headless mode

Discussão em 'Python' iniciado por Stack, Setembro 25, 2024.

  1. Stack

    Stack Membro Participativo

    I am using this docker command to create a pdf file in headless mode. It is working as expected.

    # docker run --rm -v .:/workspace shantanuo/libreoffice-converter /workspace/pm_in_paris.odt --outdir /workspace


    This is my docker file:

    # vi Dockerfile
    FROM ubuntu:latest

    RUN apt-get update && \
    apt-get install -y libreoffice

    WORKDIR /workspace

    ENTRYPOINT ["libreoffice", "--headless", "--convert-to", "pdf"]


    But I am not able to apply a template before creating a pdf file.

    FROM ubuntu:latest
    RUN apt-get update && apt-get install -y libreoffice python3 python3-venv
    RUN python3 -m venv /workspace/venv

    RUN /workspace/venv/bin/pip install --upgrade pip
    RUN /workspace/venv/bin/pip install unotools

    COPY * /workspace/
    WORKDIR /workspace

    CMD ["/workspace/venv/bin/python3", "/workspace/convert_to_pdf.py", "/workspace/paris.odt", "/workspace/template.ott", "/workspace/output.pdf"]


    I built docker image using the above mentioned file and run it:

    docker run -it shantanuo/libreoffice-converter2 bash


    I execute this command from the docker container:

    root@8a6d5682dcfa:/workspace# soffice --headless --accept="pipe,name=libreoffice;urp;StarOffice.ComponentContext" & python3 /workspace/convert_to_pdf.py /workspace/in_paris.odt /workspace/maaya.ott /workspace/


    And get this error:

    Traceback (most recent call last):
    File "/workspace/convert_to_pdf.py", line 42, in <module>
    apply_template_and_convert_to_pdf(args.document, args.template, args.output)
    File "/workspace/convert_to_pdf.py", line 22, in apply_template_and_convert_to_pdf
    document.applyTemplate(template)
    ^^^^^^^^^^^^^^^^^^^^^^
    AttributeError: applyTemplate


    It looks like applyTemplate property is not available.

    cat convert_to_pdf.py

    import uno
    import argparse

    def apply_template_and_convert_to_pdf(document_path, template_path, output_path):
    # Create the LibreOffice UNO connection
    local_context = uno.getComponentContext()
    resolver = local_context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local_context)

    ...
    ...

    document.applyTemplate(template)

    ...
    ...


    Any suggestion will be appreciated.

    Continue reading...

Compartilhe esta Página