Read Excel
1. Import Module
Import xlrd
2. Open Excel file to read data
data = Xlrd.open_workbook (' Excel.xls ')
3. Get a Worksheet
①table = data.sheets () [0] #通过索引顺序获取
②table = Data.sheet_by_index (0) #通过索引顺序获取
③table = data.sheet_by_name (U ' Sheet1 ') #通过名称获取
4. Get values for entire rows and columns (return array)
Table.row_values (i)
Table.col_values (i)
5. Get the number of rows and columns
table.nrowsTable.ncols 6, Get cell
Table.cell (0,0). ValueTable.cell (2,3). Value
Write Excel
1. Import Module
Import XLWT
2, create workbook (actually Excel, then save a bit on the line)
workbook = xlwt. Workbook (encoding = ' ASCII ')
3. Create a table
worksheet = workbook.add_sheet (' My worksheet ')
4. Write content to cell
worksheet.write (0, 0, label = ' Row 0, Column 0 Value ')
5. Save
workbook.save (' Excel_workbook.xls ')
write data to an existing Excel
From XLRD import open_workbookfrom xlutils.copy import Copy RB = Open_workbook (' M:\\1.xls ') #通过sheet_by_index () Gets the sheet without the Write () Method rs = rb.sheet_by_index (0) WB = Copy (RB) #通过get_sheet () Gets the sheet with the Write () method ws = Wb.get_sheet (0) Ws.write (0, 0, ' changed! ') wb.save (' M:\\1.xls ') |
Python operations Excel