Ask Your Question
0

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
0

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

karolus gravatar image

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

Hallo

Take the "hello World"-example from:
$LO_program_folder/share/scripts/python/

and store your extented script to:
$your_user_configfolder/Script/python/wathever.py

Karolus

edit flag offensive delete link more

Comments

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
0

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 = []
files_to_create.append("{0}.odt".format(date_string))
files_to_create.append("{0}_log.odt".format(date_string))

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

g_exportedScripts = hello_world, create_writer_files

edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 870 times

Last updated: Oct 10 '15