python中与excel操作相关的模块:
安装方式:pip install openpyxl
import xlwt
book = xlwt.Workbook()
sheet = book.add_sheet("sheet1")
sheet.write(0,0,"id")
sheet.write(1,0,"1")
book.save("test.xls") #结尾用xlsx的话,用office打开有问题,wps打开没问题
title = ["id","name","sex","city"]
students = [
[1,"xiaohei","男","北京"],
[2,"xiaolan","男","北京"],
[3,"candy","女","深圳"],
[4,"lucy","女","北京"],
]
students.insert(0,title)
row = 0 #行号
for student in students: #控制行
col = 0 # 列号
for value in student: #控制列
sheet.write(row,col,value)
col+=1
row+=1
for row,student in enumerate(students):
for col,value in enumerate(student):
sheet.write(row,col,value)
book.save("test.xls")
data = xlrd.open_workbook(table_name)
table = data.sheets()[0]
nrows,ncols = table.nrows,table.ncols
for i in range(1, nrows):
value1= table.cell(i, 1).value 获取第i行,第1列单元格数据
print(value1)