声明:自学笔记,不喜勿喷@_@~
版本python 3.7.1 操作系统windows7 编辑器sublime 3
读取Excel文件
- 引入模块
import xlrd
- 获取文件路径
flpath = r'D:\code\test.xls'
- 使用方法打开文件
book = xlrd.open_workbook(flpath)
- 根据索引找到具体的表单
sheet0 = book.sheet_by_index(0)
- 根据表单名找到该表单
sheet0 = book.sheet_by_name('员工信息表')
print(sheet0)
- 获取对应索引下的表单名字
sname = book.sheet_names()[0]
print(sname)
- 获取对应表单的总行数
nrows = sheet0.nrows
print(nrows)
for i in range(nrows):
print(sheet0.row_values(i))
- 获取对应表单的总列数
ncols = sheet0.ncols
print(ncols)
for j in range(ncols):
print(sheet0.col_values(j))
- 获取sheet0表第一行的数据
row_data = sheet0.row_values(0)
print(row_data)
- 获取sheet0表第一列的数据
col_data = sheet0.col_values(0)
print(col_data)
- 根据坐标读取数据
data = sheet0.cell_value(0,0) #获取sheet0第一行第一列的数据
print(data)
改写Excel文件
- 引入模块
import xlwt
- 创建对象
book = xlwt.Workbook(encoding='utf-8')
- 创建一张表单
sheet = book.add_sheet('员工信息表')
- 创建第二张表单
sheet1 = book.add_sheet('员工信息表1')
- 根据坐标录入数据
sheet.write(0,0,'name')
sheet.write(1,0,'zhangdada')
sheet.write(0,1,'中文名字hahahh')
sheet.write(1,1,'张大大')
- 保存路径及名字
book.save(r'd:\code\antest.xls')