Saturday, 20 August 2022

How to convert docx to pdf on Mac OS with Python?

I've looked up several SO and other web pages but I haven't found anything that works.

The script I wrote, opens a docx, changes some words and then saves it in a certain folder as a docx. However, I want it to save it as a pdf but I don't know how to.

This is an example of the code I'm working with:

# Opening the original document
doc = Document('./myDocument.docx')

# Some code which changes the doc

# Saving the changed doc as a docx
doc.save('/my/folder/myChangedDocument.docx')

The things I tried to do for it to save as a pdf:

from docx2pdf import convert

# This after it was saved as a docx
    convert('/my/folder/myChangedDocument.docx', '/my/folder/myChangedDocument.pdf')

But it says that Word needs permission to open the saved file and I have to select the file to give it the permission. After that, it just says:

0%|          | 0/1 [00:03<?, ?it/s]
{'input': '/my/folder/contractsomeVariable.docx', 'output': '/my/folder/contractsomeVariable.pdf', 'result': 'error', 'error': 'Error: An error has occurred.'}

And I tried to simply put .pdf instead of .docx after the document name when I saved it but that didn't work either as the module docx can't do that.

So does someone know how I can save a docx as a pdf using Python?



from How to convert docx to pdf on Mac OS with Python?

No comments:

Post a Comment