Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a row of Excel sheet to another sheet using Python

I want to compare the value of a given column at each row against another value, and if the values are equal, I want to copy the whole row to another spreadsheet.

How can I do this using Python?

THANKS!

like image 791
DanielY Avatar asked Dec 06 '25 16:12

DanielY


1 Answers

pls refer to python excel library xlrd(for excel reading)/xlwt(for excel writing) http://www.python-excel.org/

for example(reading)(from this):

import xlrd

fname = "sample.xls"
bk = xlrd.open_workbook(fname)
shxrange = range(bk.nsheets)
try:
    sh = bk.sheet_by_name("Sheet1")
except:
    print "no sheet in %s named Sheet1" % fname
    return None
nrows = sh.nrows
ncols = sh.ncols
print "nrows %d, ncols %d" % (nrows,ncols)

cell_value = sh.cell_value(1,1)
print cell_value

row_list = []
for i in range(1,nrows):
    row_data = sh.row_values(i)
    row_list.append(row_data)

if you are handling with Excel 2007 , then use openpyxl : http://packages.python.org/openpyxl/

like image 179
Shawn Zhang Avatar answered Dec 08 '25 04:12

Shawn Zhang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!