Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python to delete a row in excel spreadsheet

Tags:

python

excel

xlwt

I have a really large excel file and i need to delete about 20,000 rows, contingent on meeting a simple condition and excel won't let me delete such a complex range when using a filter. The condition is:

If the first column contains the value, X, then I need to be able to delete the entire row.

I'm trying to automate this using python and xlwt, but am not quite sure where to start. Seeking some code snippits to get me started... Grateful for any help that's out there!

like image 950
Jane Avatar asked Apr 12 '11 12:04

Jane


2 Answers

Don't delete. Just copy what you need.

  1. read the original file
  2. open a new file
  3. iterate over rows of the original file (if the first column of the row does not contain the value X, add this row to the new file)
  4. close both files
  5. rename the new file into the original file
like image 195
eumiro Avatar answered Sep 27 '22 22:09

eumiro


I like using COM objects for this kind of fun:

import win32com.client
from win32com.client import constants

f = r"h:\Python\Examples\test.xls"
DELETE_THIS = "X"

exc = win32com.client.gencache.EnsureDispatch("Excel.Application")
exc.Visible = 1
exc.Workbooks.Open(Filename=f)

row = 1
while True:
    exc.Range("B%d" % row).Select()
    data = exc.ActiveCell.FormulaR1C1
    exc.Range("A%d" % row).Select()
    condition = exc.ActiveCell.FormulaR1C1

    if data == '':
        break
    elif condition == DELETE_THIS:
        exc.Rows("%d:%d" % (row, row)).Select()
        exc.Selection.Delete(Shift=constants.xlUp)
    else:
        row += 1

# Before
# 
#      a
#      b
# X    c
#      d
#      e
# X    d
#      g
#        

# After
#
#      a
#      b
#      d
#      e
#      g

I usually record snippets of Excel macros and glue them together with Python as I dislike Visual Basic :-D.

like image 44
Fenikso Avatar answered Sep 27 '22 22:09

Fenikso