import xlrd, xlwt
# 打开Excel文件读取数据
workbook = xlrd.open_workbook('data.xlsx')
# 通过sheet页索引顺序或名称获取sheet页
sheet = workbook.sheets()[0]
# sheet = workbook.sheet_by_index(1)
# sheet = workbook.sheet_by_name(u'3班') #防止中文乱码,在字符串前加'u'
print(sheet, type(sheet))
print(sheet.nrows, sheet.ncols) #打印sheet页数据的总行数,总列数
for i in range(sheet.nrows): #按行打印数据
a = sheet.row_values(i)
print(a)
for j in range(sheet.ncols): #按列打印数据
b = sheet.col_values(j)
print(b)
for m in range(sheet.nrows): #打印单元格数据
for n in range(sheet.ncols):
# print(table1.cell_value(m, n))
print(sheet.cell(m, n).value)
sheetdata = []
for m in range(sheet.nrows): #按列表输出每行数据
linedata = []
for n in range(sheet.ncols):
linedata.append(sheet.cell(m,n).value)
sheetdata.append(linedata)
print(sheetdata)
# 写Excel
workbook = xlwt.Workbook(encoding='utf-8')
sheet = workbook.add_sheet('数据', cell_overwrite_ok=True)
for x in range(5):
for y in range(5):
value = x+y
sheet.write(x, y, value)
workbook.save(r'E:\Python\write.xlsx')