Ask Your Question

Default PDF Filename

asked 2017-12-02 00:52:13 +0100

Timberline gravatar image

updated 2017-12-02 01:34:12 +0100

As a programmer, what would I need to know and look up (and where) in order to add a timestamp (with punctuation deleted or modified, or maybe just the epoch time) to the default filename used for exporting from LibreWriter to PDF? I'd be happy either with being able to change the default appearing in the standard dialog or with being able to create a button that would save directly without a dialog, just using the same directory as the source file (or relative to that directory) and injecting the timecode into the existing source file name when creating the PDF.

What method would be most resilient when doing version updates?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-12-02 16:50:34 +0100

Jim K gravatar image

Keep the code below in the LO user directory. To find this location, go to Tools -> Options -> LibreOffice -> Paths and look at AutoCorrect for example. The user directory is kept when updating versions.

For how to run python macros, see Primarily, this involves putting the code in a new directoryuser/Scripts/python (case must match).

To set up a toolbar button to run the macro, go to Tools -> Customize.

import datetime
import uno
from import PropertyValue

def export_pdf_with_timestamp():
    oDoc = XSCRIPTCONTEXT.getDocument()
    sURL = oDoc.getURL()
    ext_len = len(".odt")
    timestamp = "{:%Y-%m-%d_%H-%M-%S}".format(
    outfilename = "%s_%s.pdf" % (sURL[:-ext_len], timestamp)
    ctx = XSCRIPTCONTEXT.getComponentContext()
    smgr = ctx.ServiceManager
    frame = oDoc.getCurrentController().getFrame()
    dispatcher = smgr.createInstanceWithContext(
        "", ctx)
    props = tuple(PropertyValue() for dummy in range(2))
    props[0].Name = "URL"
    props[0].Value = outfilename
    props[1].Name = "FilterName"
    props[1].Value = "writer_pdf_Export"
    dispatcher.executeDispatch(frame, ".uno:ExportToPDF", "", 0, props)

g_exportedScripts = export_pdf_with_timestamp,
edit flag offensive delete link more


Thank you. This looks like it will be straightforward to implement when I get a little break in the coming week. It's much more thorough help than I expected and the feature will be a big help in my workflow.

Timberline gravatar imageTimberline ( 2017-12-03 07:26:53 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-12-02 00:52:13 +0100

Seen: 212 times

Last updated: Dec 02 '17