1. execl open and get sheet
First you need to install two libraries:
- xlrd for reading
- xlwt is used to write
Open the excel file:
- xlrd.open_workbook("xxx.xls")
Get the sheet:
import xlrd
workbook = xlrd.open_workbook('成绩表.xlsx')
# 获取sheet名字
print(workbook.sheet_names())
sheet = workbook.sheet_by_index(0)
print(sheet.name)
sheet = workbook.sheet_by_name('1班')
print(sheet.name)
sheets = workbook.sheets()
for sheet in sheets:
print(sheet.name)
# 获取指定sheet的行列列数
sheet = workbook.sheet_by_index(0)
print({"rows": sheet.nrows, "cols": sheet.ncols})
2. Get the cell and its attributes
# 获取指定sheet的行列列数
sheet = workbook.sheet_by_index(0)
print({"rows": sheet.nrows, "cols": sheet.ncols})
sheet = workbook.sheet_by_index(0)
cell = sheet.cell(0, 0)
print(cell.value)
cell_value = sheet.cell_value(0, 1)
print(cell_value)
cells = sheet.row_slice(1, 1, 4)
for cell in cells:
print(cell.value)
cells = sheet.col_slice(0, 1, sheet.nrows)
for cell in cells:
print(cell.value)
cell_values = sheet.col_values(1, 1, sheet.nrows)
print(cell_values)
cell_values = sheet.row_values(1, 1, sheet.ncols)
print(cell_values)
print(sum(cell_values), sum(cell_values)/3.0)
3. Cell common data types
# cell类型
sheet = workbook.sheet_by_index(0)
cell = sheet.cell(0, 0)
print(cell.ctype)
print(xlrd.XL_CELL_TEXT)
cell = sheet.cell(1, 1)
print(cell.ctype)
print(xlrd.XL_CELL_NUMBER)
cell = sheet.cell(20, 0)
print(cell.ctype)
print(xlrd.XL_CELL_DATE)
cell = sheet.cell(19, 0)
print(cell.ctype)
print(xlrd.XL_CELL_BOOLEAN)
cell = sheet.cell(0, 0)
print(cell.ctype)
print(xlrd.XL_CELL_EMPTY)
4. Excel writing steps
import xlwt
import random
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('sheet1')
headers = ['姓名', '语文', '英语', '数学']
for index, header in enumerate(headers):
sheet.write(0, index, header)
names = ['张三', '李四', '王五']
scores = []
for index, name in enumerate(names):
sheet.write(index+1, 0, name)
for row in range(1, 4):
for col in range(1, 4):
sheet.write(row, col, random.randint(90, 100))
workbook.save('成绩表2.xls')
5. Edit excel file
- Read the original excel file first
- Read the above to modify the cell, use sheet.put_cell(row, col, ctype, value, None) to achieve
- Re-create an excel file and write the previously read data into the new excel
import xlwt, xlrd
rwb = xlrd.open_workbook('成绩表.xlsx')
rsheet = rwb.sheet_by_index(0)
rsheet.put_cell(0, 4, xlrd.XL_CELL_TEXT, '总分', None)
nrows = rsheet.nrows
for row in range(1, nrows):
grades = rsheet.row_values(row, 1, 4)
print(grades)
print(type(grades[0]))
total = sum(grades)
rsheet.put_cell(row, 4, xlrd.XL_CELL_NUMBER, total, None)
nrows = rsheet.nrows
ncols = rsheet.ncols
for col in range(1, 5):
grades = rsheet.col_values(col, 1, nrows)
avg = sum(grades) / len(grades)
rsheet.put_cell(nrows, col, xlrd.XL_CELL_NUMBER, avg, None)
wwb = xlwt.Workbook()
wsheet = wwb.add_sheet('sheet1')
for row in range(0, rsheet.nrows):
for col in range(0, rsheet.ncols):
wsheet.write(row, col, rsheet.cell_value(row, col))
wwb.save('abc.xls')