[Python] Docx to pdf using pandoc in python

Discussão em 'Python' iniciado por Stack, Abril 6, 2025.

  1. Stack

    Stack Membro Participativo

    So I a quite new to Python so it may be a silly question but i can't seem to find the solution anywhere.

    I have a django site I am running it locally on my machine just for development. on the site I want to convert a docx file to pdf. I want to use pandoc to do this. I know there are other methods such as online apis or the python modules such as "docx2pdf". However i want to use pandoc for deployment reasons.

    I have installed pandoc on my terminal using brew install pandoc. so it should b installed correctly.

    In my django project i am doing:

    import pypandoc
    import docx

    def making_a_doc_function(request):
    doc = docx.Document()
    doc.add_heading("MY DOCUMENT")
    doc.save('thisisdoc.docx')
    pypandoc.convert_file('thisisdoc.docx', 'docx', outputfile="thisisdoc.pdf")
    pdf = open('thisisdoc.pdf', 'rb')
    response = FileResponse(pdf)
    return response


    The docx file get created no problem but it not pdf has been created. I am getting an error that says:

    Pandoc died with exitcode "4" during conversion: b'cannot produce pdf output from docx\n'

    Does anyone have any ideas?

    Continue reading...

Compartilhe esta Página