Python:操作Excel表格
读操作:
import xlrd
f = xlrd.open_workbook('F:\\pythontest\\test.xlsx')#打开excel表
sheet1 = f.sheets()[0]#读取excel文件中的第0个sheet,也就是sheet1
sheet1.row_values(0)#对每一行的值进行操作
sheet1.row(0)#第1行中每个单元格的类型+值
#xlrd中cell(x,y)的类型分类 空:0,字符串:1,数字:2,日期:3,布尔:4,error:5
sheet1.row_types(0)#第1行中每个单元格的类型
print(sheet1.cell(0,0).value)#读取第一行第一列的值
print(sheet1.cell(0,0).ctype)#读取第一行第一列的值的类型
print(sheet1.cell_value(0,0))#读取第一行第一列的值
print(sheet1.cell_type(0,0))#读取第一行第一列的值的类型
写操作:
import xlwt
sheet1 = f.sheets()[0]
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('表格1',cell_overwrite_ok=True)
worksheet.write(2, 2, '此处为值')
workbook.save('F:\\pythontest\\test1.xlsx')