How can I export a PDF in a Python Macro?

I am writing a python macro and I need to export my file to PDF. How do I do it?

Well, it is actually pretty simple:

#!/usr/bin/python3
import uno
from com.sun.star.beans import PropertyValue
def make_pdf():
    properties=[] 
    p=PropertyValue() 
    p.Name='FilterName' 
    p.Value='calc_pdf_Export' 
    properties.append(p) 
    oDoc = XSCRIPTCONTEXT.getDocument()
    oDoc.storeToURL('file:///tmp/test.pdf',tuple(properties))

It will save a PDF version of your document into /tmp/test.pdf (on Linux, I am not sure what it would do on Windows).

This is based on this thread

It fails – because on Windows there is no path like this – what do you expect ?

Well, my knowledge of Windows is rather limited, so I was not sure. I guess replacing “/tmp/test.pdf” for “C:/test.pdf” or something should work, but I am not even sure what a proper path on windows is, I have not used it almost at all since over ten years:-).

Also on Windows you must have WritingAccess on the Path the Path above should throw an “PermissionError”

python should work with /slashes instead of \backslashes like "C:/path/to/your/filename.txt"
if you want \backslashes mark the path as rawstring like r"C:\path\to\your\filename.txt"
or escape every \ by itself "C:\\path\\to\\your\\filename.txt"

consider you script above use an API-method to write the pdffile,
API uses mostly FileURLS : file:///C:/path/to/your/filename.txt