-1

How do I open an existing excel file, write to it, and save it as the same filename. None of the previous data should be lost and the new data should be saved.

The pseudocode would be as follows:

open excel file
write data to last row
save excel file
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
user1681664
  • 1,771
  • 8
  • 28
  • 53

1 Answers1

4

If you are willing to use csv format (has obvious limitations) you can use Python's built in CSV library.

Here is a short reader example:

 >>> import csv
 >>> with open('eggs.csv', 'rb') as csvfile:
 ...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
 ...     for row in spamreader:
 ...         print ', '.join(row)
 Spam, Spam, Spam, Spam, Spam, Baked Beans
 Spam, Lovely Spam, Wonderful Spam

Writer example:

import csv
with open('eggs.csv', 'a') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

Keep in mind with csv, you can only modify spreadsheet data, no fancy graphs or anything.

daiuto
  • 486
  • 2
  • 10
  • 20