0

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.

strongbad
  • 133
  • 1
  • 4
  • 12
  • I'm not sure where this should be applied, because I didn't understand exactly what you are doing, but would `new_template = template[:template.rfind('}')]` work for you? It takes the template and removes everything from the last bracket to the end of the string. – Paulo Almeida Sep 03 '15 at 11:00
  • Thank you, this is what I was trying to achieve but wasn't sure where it should be implemented. – strongbad Sep 07 '15 at 09:55

1 Answers1

1

This code

with open(rtf_filename, "a") as f:
    f.write(template_append % data_source)

simply appends directly onto the end of the newly opened file.

Since these appear to be small files, it will be easiest if you just read the file into a list, remove the closing } at the end of the list, append the new data to the list, and finally write the file back out. You could overwrite the file in place, or use a temporary file and then replace the original file with the temporary file as shown here:

import shutil
from tempfile import NamedTemporaryFile

rtf_filename = time.strftime("%d") + ".rtf" # rtf filename in date format
template = open("template.rtf").read() # rtf template
template_append = open("template_append.rtf").readlines()
data_source = {'data': 'test_data'}

if os.path.isfile(rtf_filename) is True: # RTF file might exist
    with open(rtf_filename) as rtf_file, NamedTemporaryFile(dir='.', delete=False) as tmp_file:
        lines = rtf_file.readlines()[:-1]    # reads all lines and truncates the last one
        lines.extend([s % data_source for s in template_append])
        tmp_file.writelines(lines)
    shutil.move(tmp_file.name, rtf_filename)
else:
    with open(rtf_filename, "w") as f:
        f.write(template % data_source)
mhawke
  • 84,695
  • 9
  • 117
  • 138