0

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'))
amchugh89
  • 1,276
  • 1
  • 14
  • 33
  • 2
    ```xlwt``` can absolutely do this – wnnmaw Aug 18 '14 at 17:29
  • The general scheme would be use ```xlrd``` to get the number of rows and then just write to the row after that – wnnmaw Aug 18 '14 at 18:09
  • Yeah I started doing that, it copied the data ok, but now I need to find a way to preserve the style (background colors and stuff) – amchugh89 Aug 18 '14 at 18:15
  • You should always include details like that in your original question. Could you please update your question to say exactly what you're trying to do, what you've tried to do (specifically), and problem you're having (included actual and expected results) – wnnmaw Aug 18 '14 at 18:26

1 Answers1

0

xlwt helps you in writing to excel.

To write anything to excel you have to specify a row and column So its like worksheet.write(x,y,x*y) This commands writes to a cell with x, y co-ordinates the values of x*y.

So, in your case, to write to a new row, just give the row number where you want the new row, and write as much as columns you want. Easy.

Its not a list that you need to append you to. You can jump of to any cell you want to and write.

Check out a useful example here - http://codingtutorials.co.uk/python-excel-xlrd-xlwt/

iamkhush
  • 2,562
  • 3
  • 20
  • 34