Ask Your Question

How to create new files with a python macro [closed]

asked 2014-08-14 23:11:50 +0200

ajo3d gravatar image

Hi, I want to write a macro that will create 2 new Writer files when run. One titled "day.month.year" and the other "log day.month.year". Today it would be 8.14.2014 and log 8.14.2014. I would prefer to do this in python but I'm open to alternatives. What is the function call / method I would use to create a new Writer file from a python script?

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-18 03:32:44.890402

2 Answers

Sort by » oldest newest most voted

answered 2014-08-15 09:42:53 +0200

karolus gravatar image

updated 2014-08-15 09:43:28 +0200


Take the "hello World"-example from:

and store your extented script to:


edit flag offensive delete link more


Note, you may have to create that folder if it doesn't exist already. You'll also have to quit and restart LibreOffice in order for it to see your new file.

John_T gravatar imageJohn_T ( 2015-10-10 13:43:48 +0200 )edit

answered 2015-10-10 15:14:03 +0200

John_T gravatar image

Below is a very simple example. If you place the following code in a file in the folder described above, quit and restart libre office, and then run libre office writer again, you will have two functions available for the the Tools>Macro>Run Macro option:

The function "hello_world" will place some text into the open writer document The function "create_writer_files" will create two files in the directory specified in "path_to_files"

Notes: 1. This will overwrite the file if it already exists. 2. The file it creates is an empty file, while it has an odt extension, it isn't really an odt file at this point

def hello_world():
XSCRIPTCONTEXT.getDocument().getText().setString(r"""Hello World!""")

def create_writer_files(*args):
"""Creates two writer files based on the date"""

date_string = "10-10-2015"
path_to_files = "/home/john/Documents/"

files_to_create = []

for each_file in files_to_create:
    full_path ="{0}/{1}".format(path_to_files, each_file)
    new_file = open(full_path, "w")

g_exportedScripts = hello_world, create_writer_files

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2014-08-14 23:11:50 +0200

Seen: 870 times

Last updated: Oct 10 '15