python---操作Excel模块

import shlex

import openpyxl #能读能写
#
# xlrd 只能写 32000
# xlwt 只能读

# book = openpyxl.workbook()
# sheet = book.create_sheeet("sheet1") #创建一个新的sheet
# sheet.append([1,2,3]) #写一整行 单元格1 单元格2
# sheet.append([4,5,6])
# sheet=book.active #获取默认的sheet
# sheet.cell(1,1,"编号")#指定写入某个单元格
# book.dave("number.xls")

#写入Excel 上一个题信息
# import openpyxl
# name_dict = {}
# names = s.split()
# book = openpyxl.Workbook()
# sheet = book.active
# sheet.append(["姓名","账号"])
# for name in names:
# pinyin = p.get_pinyin(name,'')
# if pinyin not in name_dict:
# name_dict[pinyin] = 1
# else:
# name_dict[pinyin]+=1
# pinyin_count = name_dict[pinyin]
# if pinyin_count!=1:
# pinyin = "%s%s" % (pinyin,pinyin_count-1)
# sheet.append([name,pinyin])
# book.save("用户.xlsx")

#Excel

book = openpyxl.load_workbook("用户.xlsx") #必须是xlsx结尾的
# sheet = book.active #获取默认sheet
sheet = book["Sheet"] #指定的sheet 某一个
# print(sheet.max_row) #多少行
# print(sheet.max_column) #多少列

# print(list(sheet.values)) #所有行
# for v in sheet.values:#获取所有行
# print(v)
#
# #for row in sheet.rows: #获取所以的行,还得去循环
# row=[r.value for r in row ]
# print(row)
#
# for col in sheet.columns:#获取所有的列
# col = [r.value for r in col]
# print(col)

# print(sheet.cell(1,1).value)#获取指定单元格的内容,第一行第一列
#
import xlrd #只能读 从0开始
#只能读32000
book = xlrd.open_workbook("用户.xlsx")
sheet = book.sheet_by_index(0) #根据下标来
#sheet = book.sheet_by_name("sheet") #根据名字来
print(sheet.cell(0,0).value) #获取指定单元格的内容,是从0开始的
print(sheet.nrows) #总共多少行
print(sheet.ncols) #总共多少列

# print(sheet.row_values(0))#指定获取某一行的
# print(sheet.col_values(0))#指定获取某一列

for i in range(sheet.nrows):#获取所有行的数据,总共多少行循环多少次
print(sheet.row_values(i))

enumerate()用法:实现下标和值对应


例:


l=["lxp","may","candy"]
print(list(enumerate(l)))
结果:[(0, 'lxp'), (1, 'may'), (2, 'candy')]


posted @ 2021-10-23 09:58  王王的王  阅读(309)  评论(0)    收藏  举报