Is there any way to append a new row to an Excel spreadsheet using Python? -
i have searched around, tried win32com
, xlrd
/xlwt
/xlutils
can insert data existing excel rows - want able insert 1 new row (specifically first one, in case). know how using python?
as per suggestion, include did add row excel file
from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd xlutils.copy import copy # http://pypi.python.org/pypi/xlutils xlwt import easyxf # http://pypi.python.org/pypi/xlwt import xlwt
...next part indented because it's in loops, not @ stack overflow formatting
rb = open_workbook( os.path.join(cohort_path, f),on_demand=true,encoding_override="cp1252",formatting_info=true) #the following because messed file has missing row if f=='messedup.xls': r_sheet = rb.sheet_by_name('sheet name') # read copy introspect file wb = copy(rb) w_sheet = wb.get_sheet(rb.sheet_names().index('sheet name')) #workaround #fix first rows col_index in range(0, r_sheet.ncols): row_index in range(2, r_sheet.nrows): xfx = r_sheet.cell_xf_index(row_index-1, col_index) xf = rb.xf_list[xfx] bgx = xf.background.pattern_colour_index xlwt.add_palette_colour("custom_colour", 0x17) #rb.set_colour_rgb(0x21, 251, 228, 228) #or wb?? style_string = 'pattern: pattern solid, fore_colour custom_colour' if bgx in (55,23) else none style = xlwt.easyxf(style_string) w_sheet.write(row_index, col_index, r_sheet.cell(row_index-1,col_index).value,style=style) wb.save(os.path.join(cohort_path, 'fixed_copy.xls'))
xlwt helps in writing excel.
to write excel have specify row , column worksheet.write(x,y,x*y)
commands writes cell x, y co-ordinates values of x*y.
so, in case, write new row, give row number want new row, , write as columns want. easy.
its not list need append to. can jump of cell want , write.
check out useful example here - http://codingtutorials.co.uk/python-excel-xlrd-xlwt/
Comments
Post a Comment