1

my code in Python should generate a continous Serial-No. after doing a raw input and save it in a new row in Excel. But i have one problem: When i run the programme, it always overwrites the last run. So how can i open (read) the existing Excelsheet, write new data below the existing data without overwriting?

My code is like this:

import xlsxwriter
workbook  = xlsxwriter.Workbook('file.xlsx')
worksheet = workbook.add_worksheet()

row_count = 0
col = 0
n1 = 99
n2 = 99
n3 = 99
value_manufacturer = "0"

while True:
inp = raw_input('Choose the system:')                       
if inp == '1':                                              
    print 'HS25 is selcted'                                 
    serial_no_HS25 = "23"                                  
    break

while True:
inp = raw_input('Is the system OK?')                        
if inp == '4':                                              
    n1 += 1
    print (serial_no_HS25 + value_manufacturer + str(n1))   
    row_count += 1                                          
    if (n1) <= 999:                                                                             
        worksheet.write(row_count, col, serial_no_HS25 + value_manufacturer + str(n1))         
        worksheet.write('C3', row_count)
    else:                                                                                        
        worksheet.write(row_count, col, (n1)
else:
    print ('Wrong input for HS25')

The code works but how to insert any memory funtion or something similar? Thank you for your help!!!

Aske Doerge
  • 1,331
  • 10
  • 17
  • xlsxwriter cannot be used to modify an existing excel file. It's for writing new files. http://stackoverflow.com/a/18850144/2744166 – Joe Young Oct 26 '15 at 14:08
  • Sry i expressed it wrong: I write a new file but i want to open and write in the sheet over and over again. So my Python programme should remember the data it has written before and write the current Serial-No. below the last written line. – Andreas Braun Oct 26 '15 at 14:27
  • Yes, i understand. But xlsxwriter will not append to the sheet. It will overwrite everything in the sheet because it was not made for updating, only writing completely new files. You will need to use a different module. – Joe Young Oct 26 '15 at 14:28
  • Ok, thank you. Which module is able to read and write at once? – Andreas Braun Oct 26 '15 at 15:03
  • openpyxl. See the answer I linked in the first comment. – Joe Young Oct 26 '15 at 15:04

0 Answers0