How to copy excel sheet row to another sheet using Python

I want to compare the value of a given column in each row with a different value, and if the values ​​are equal, I want to copy the entire row to another table.

How to do it using Python?

THANKS!

+4
source share
2 answers

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

e.g. (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 working with Excel 2007, use openpyxl : http://packages.python.org/openpyxl/

+3
source

For "xls" files, you can use the xlutils package. It is currently not possible to copy objects between workbooks in openpyxl due to the structure of the Excel format: there are many dependencies around the place that you need to manage. Therefore, the responsibility of the client code is to copy everything you need manually. If time permits, we can try to port some of the xlutils functions to openpyxl.

0
source

All Articles