I use an RTF template for data I'm gathering:
{\rtf1\ansi\deff0 {\fonttbl {\f0 Monotype Corsiva;}}
\f0\fs28 Here be %(data)s
}
I have a seperate template for append operation that is just like above without the first line.
The challenge is that when the file is created, there might be additional data for the same filename later on. Appending data to a file can be done but I haven't found a way of getting rid of the "}" EOF syntax if the file exists. Here's the code for appending the data without removing the last line:
rtf_filename = time.strftime("%d") + ".rtf" # rtf filename in date format
template = open("template.rtf").read() # rtf template
template_append = open("template_append.rtf").read() # rtf append template
data_source = {"data": "test_data"} # just a string to keep this simple
if os.path.isfile(rtf_filename) is True: # RTF file might exist
with open(rtf_filename, "a") as f:
f.write(template_append % data_source)
else:
with open(rtf_filename, "w") as f:
f.write(template % data_source)
file output for the code above:
{\rtf1\ansi\deff0 {\fonttbl {\f0 Monotype Corsiva;}}
\f0\fs28 Here be test_data
}\f0\fs28 Here be test_data # line where "}" should be removed
}
Thanks for any help.