Ask Your Question

How can I export a PDF in a Python Macro? [closed]

asked 2014-08-26 00:49:52 +0200

sup gravatar image

updated 2014-08-26 00:51:39 +0200

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

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2016-03-02 23:50:56.148762

1 Answer

Sort by » oldest newest most voted

answered 2014-08-26 00:55:03 +0200

sup gravatar image

Well, it is actually pretty simple:

import uno
from import PropertyValue
def make_pdf():
    oDoc = XSCRIPTCONTEXT.getDocument()

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

edit flag offensive delete link more



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

karolus gravatar imagekarolus ( 2014-08-26 02:28:18 +0200 )edit

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:-).

sup gravatar imagesup ( 2014-08-26 02:44:51 +0200 )edit

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

karolus gravatar imagekarolus ( 2014-08-26 09:11:12 +0200 )edit

Question Tools

1 follower


Asked: 2014-08-26 00:49:52 +0200

Seen: 662 times

Last updated: Aug 26 '14