xlrd, xlwt 用来处理xls,支持Excel2003版,pyexcel处理xlsx,支持excel2007版
我这里我用xlrd,xlwt来生成excel,可能会存在不兼容,但是功能没有问题
用的python3,因为python2会有乱码问题。
# -*- coding: utf-8 -*-
import xlrd, xlwt
def read():
data = xlrd.open_workbook("D:\pyproject\python_excel\1.xlsx") #打開已存在的excel
# data.sheets()
tables = data.sheet_names() #获取sheet表名字
for table in tables:
data1 = xlwt.Workbook()
sheet1 = data1.add_sheet('汇总', cell_overwrite_ok=True) #写入excel
sheet = data.sheet_by_name(table)
print(table)
a = sheet.col_values(1, 1) #读取对应列的数据
b = sheet.col_values(4, 1)
c = sheet.col_values(7, 1)
print(a, b, c)
for i in range(0, len(a)):
sheet1.write(i, 0, a[i]) #在excel中写入对应位置
sheet1.write(i, 1, b[i])
sheet1.write(i, 2, c[i])
data1.save('D:\pyproject\python_excel\2.xls')
if __name__ == '__main__':
read()