python读取execl数据
1.导入模块
import xlrd
2.取出表格名
workbook = xlrd.open_workbook(r'D:python视频cmdb-demo.xlsx') name = workbook.sheet_names() print(name)
3.按照坐标取值
import xlrd workbook = xlrd.open_workbook(r'D:python视频cmdb-demo.xlsx')
index = workbook.sheet_by_index(0)
value = index.cell_value(0,1)
print(value)
4.表格一块有几行
import xlrd workbook = xlrd.open_workbook(r'D:python视频cmdb-demo.xlsx') index = workbook.sheet_by_index(0) info = index.nrows print(info)
5.按行读取内容,返回列表。
import xlrd
workbook = xlrd.open_workbook(r'D:python视频cmdb-demo.xlsx')
sheet1 = workbook.sheet_by_name('Sheet1')
cols = sheet1.col_values(0)
print(cols)
6.按列读取内容。
import xlrd workbook = xlrd.open_workbook(r'D:python视频cmdb-demo.xlsx') sheet1 = workbook.sheet_by_name('Sheet1') rows = sheet1.row_values(0) print(rows)
7.读取所有内容。
import xlrd workbook = xlrd.open_workbook(r'D:python视频cmdb-demo.xlsx') sheet1 = workbook.sheet_by_name('Sheet1') index = workbook.sheet_by_index(0) info = index.nrows for i in range(info): line = sheet1.row_values(i) print(line)
python往入execl表格写入数据
在第一行写入数据
import xlwt f = xlwt.Workbook(encoding='utf-8') sheet1 = f.add_sheet('sheet1') row = [u'姓名',u'年龄',u'性别',u'班级'] for i in range(0,len(row)): sheet1.write(0,i,label = row[i]) f.save('D:python视频demo.xls')