从excel表中读取数据

#首先,要安装openpyxl 命令是 pip install openpyxl 这里还有个问题(pycharm3.4没发现这个问题,在最新版本的里面有这个问题),就是你在CMD里面安装了这个py文件,然后呢,pycharm里面你就是引用不了,百度了下,要在pycharm里面设置一下,file >>>srttings >>> project interpreter 里面把这个加上,就像这样
justlikethis

切记文件的后缀要是 .xlsx 一定要自己copy过来,或者在这个模块的文件夹下面进行创建也可以

from openpyxl import load_workbook

打开excel

wb = load_workbook(“readfromexcel.xlsx”)#返回的是一个工作簿

定位表单

sheet = wb.get_sheet_by_name(“name”) 这个看源码,说是已经过时了 @deprecated(“Use wb[sheetname]”)

sheet = wb[“name”]

定位单元格

res=sheet.cell(3,3).value#cell是单元格的意思 cell(行数,列数)

print(res)

修改值,写入之前一定要把文件关闭

sheet.cell(3,3).value=“change”#就是个赋值语句

然后保存

wb.save(“readfromexcel.xlsx”)

testdata =[]
for i in range(2,6):
sub_data=[]
for j in range(1,6):
# print(sheet.cell(i,j).value)#哎呀,这个.value不要忘了啊
sub_data.append(sheet.cell(i,j).value)
testdata.append(sub_data)
print(testdata)

res1 = sheet.cell(3,3).value

print(res1)

这时候结果就是这样的
这个结果6不6

猜你喜欢

转载自blog.csdn.net/minnersinger/article/details/88795870