I have searched around, tried some win32com
and some xlrd
/xlwt
/xlutils
but all I can do is insert data into the existing Excel rows - I want to be able to insert one new row (specifically the first one, in my case). Does anyone know how to do this using Python?
as per suggestion, I will include what I did to add a row to my excel file
from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd
from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils
from xlwt import easyxf # http://pypi.python.org/pypi/xlwt
import xlwt
...next part is indented because it's in some for loops, not good at stack overflow formatting
rb = open_workbook( os.path.join(cohort_path, f),on_demand=True,encoding_override="cp1252",formatting_info=True)
#The following is because Messed up file has a missing row
if f=='MessedUp.xls':
r_sheet = rb.sheet_by_name('SHEET NAME') # read only copy to introspect the file
wb = copy(rb)
w_sheet = wb.get_sheet(rb.sheet_names().index('SHEET NAME')) #Workaround
#fix first rows
for col_index in range(0, r_sheet.ncols):
for 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'))