xlwt - Changing Excel Sheet every time python script runs -


i need change sheet in excel workbook, many times code runs..suppose python scripts runs first time , data gets saved in sheet a, next time when application runs script data should saved in sheet b.sheet should in workbook.. posible ? if yes ,how? here code:

#!/usr/bin/env python import subprocess import xlwt process=subprocess.popen('test_project.exe',stdout=subprocess.pipe) out,err = process.communicate() wb=xlwt.workbook() sheet=wb.add_sheet('sheet_a') #next time should save in sheet_b row = 0 line in out.split('\n'):      i,wrd in enumerate(line.split()):             if not wrd.startswith("***"):             print wrd            sheet.write(row,i,wrd)     row=row+1  wb.save('dds.xls')  

any appreciated...

i recommend using openpyxl. can read and write xlsx files. if needed, can convert them xls excel or open/libreoffice, assuming have 1 big file @ end.

this script creates new excel file if none exists , adds new sheet every time run. use index + 1 sheet name (title) starting 1. numerical index starts @ 0. end file has sheets named 1, 2, 3 etc. every time write data last sheet.

import os  openpyxl import workbook openpyxl.reader.excel import load_workbook  file_name = 'test.xlsx'  if os.path.exists(file_name):     wb = load_workbook(file_name)     last_sheet = wb.worksheets[-1]     index = int(last_sheet.title)     ws = wb.create_sheet(index)     ws.title = str(index + 1) else:     wb = workbook()     ws = wb.worksheets[0]     ws.title = '1' ws.cell('a2').value= 'new_value' wb.save(file_name) 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -