Ask Your Question
0

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

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

sup gravatar image

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

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
0

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

sup gravatar image

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

edit flag offensive delete link more

Comments

1

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

karolus gravatar imagekarolus ( 2014-08-26 02:28:18 +0100 )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 +0100 )edit
1

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 +0100 )edit

Question Tools

1 follower

Stats

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

Seen: 724 times

Last updated: Aug 26 '14