excel - Python Modify data of existing .xlsx file -
i have code here in python created .xlsx file using openpyxl.
however, when tried modify file, new data register file previous data gone. heard of using deepcopy or (.copy.copy) copy data of file how can paste data copied plus current edits .xlsx file?
(*some code missing here program gui, code juz far long)
##############creating###################### try: wb_id = load_workbook('list.xlsx') ws_id = wb_id.active pass except environmenterror e: # oserror or ioerror... filenotfounderror exist in python 3.x version print(os.strerror(e.errno)) #use operating system error define filenotfounderrorerror openpyxl import workbook #import workbook function create new xlsx (excel) file wb_id = workbook() ws_id = wb_id.active ws_id['a1'] = "ic" ws_id.merge_cells('b1:e1') ws_id['b1'] = "name" ws_id.merge_cells('f1:k1') ws_id['l1'] = "email" ws_id['m1'] = "height" ws_id['n1'] = "gender" ws_id['k1'] = "bloodtype" ws_id.merge_cells('o1:q1') ws_id['o1'] = "default consultation day" ws_id.merge_cells('r1:t1') ws_id['r1'] = "latest appoinment" wb_id.save("list.xlsx") pass ############editing######################### wb = load_workbook(filename='list.xlsx') ws = wb.active last_row = 1 while true: last_row += 1 cellchecker =ws['a'+str(last_row)].value #get value of cell print(cellchecker) print last_row if cellchecker none: #check if cell empty-->then row number new row wb.save('list.xlsx') break else: continue print(str(last_row)) #convert string var before passing var worksheet function openpyxl import workbook wb = workbook() ws = wb.active ws['a'+str(last_row)] = (str(entry_ic.get().strip(' '))) ws.merge_cells('b'+str(last_row)+':e'+str(last_row)) ws['b'+str(last_row)] = (str(entry_name.get())) ws.merge_cells('f'+str(last_row)+':k'+str(last_row)) ws['f'+str(last_row)] = (str(entry_email.get().strip(' '))) ws['l'+str(last_row)] = (str(entry_height.get().strip(' '))) ws['m'+str(last_row)] = gender_selected ws['n'+str(last_row)] = bloodtype_selected ws.merge_cells('o'+str(last_row)+':q'+str(last_row)) ws['o'+str(last_row)] = str(default_selected_day) ws.merge_cells('r'+str(last_row)+':t'+str(last_row)) today = datetime.date.today() #as u might wonder why redeclare var since exist, local var function ws['t'+str(last_row)] = (str(today)) wb.save('list.xlsx')
noticed editing part overwrite existing data warned in openpyxl documentation. couldn't find way modify existing .xlsx file. please me out, i'm stuck here.
i've run problem several times, , haven't been able solve using pure python; however, can use following code call vba macro python script, can used modify existing excel file.
this has allowed me come creative ways streamline work don't ever have open excel files in order update them, if data processing or gathering requires python.
# run excel macro python import win32com.client fn = '//path/filename.xlsm' xl=win32com.client.dispatch("excel.application") xl.application.visible = true xl.workbooks.open(filename = fn,readonly = 1) xl.application.run("macroname") xl=0
Comments
Post a Comment