首页 > 编程语言 >Python操作Excel(openpyxl)

Python操作Excel(openpyxl)

时间:2022-12-20 18:00:10浏览次数:36  
标签:sheet openpyxl Python list Excel value cell data

1.引入openpyxl库

安装openpyxl库:pip install openpyxl

引入openpyxl库:from openpyxl import load_worbook

2.代码实现

from openpyxl import load_workbook
#打开Excel
wb = load_workbook("C:\\Users\\Administrator\\Desktop\\testdemo.xlsx")
#定位表单
sheet = wb["s1"]
#定位单元格 行列值
print("获取最大行数:",sheet.max_row)
print("获取最大列数:",sheet.max_column)
# #遍历
test_list = []#列表
for a in range(2,sheet.max_row+1):
    test_dic = {}  # 字典
    for b in range(1,sheet.max_column+1):
        #获取指定单元格的值:sheet.cell(行,列).value
        #将获取到的数据添加到字典
        test_dic["method"]=sheet.cell(a,1).value
        test_dic["url"]=sheet.cell(a,2).value
        test_dic["data"]=eval(sheet.cell(a,3).value)#eavl() 把数据类转换成 原本数据类型
        test_dic["expect"]=sheet.cell(a,4).value
    test_list.append(test_dic)#将字典添加到列表

print(test_list)
输出:
最大行数: 3
最大列数: 4
[{'method': 'get', 'url': 'http://www.qabujiaban.com/user/login', 'data': {'username': 'uuuu222都44', 'password': 'WJHasb124*1'}, 'expect': '0000'}, {'method': 'get', 'url': 'http://www.qabujiaban.com/user/login', 'data': {'username': 'uuuu222都44', 'password': 'WJHasb124*1'}, 'expect': '0000'}]

3.代码封装

#引入仓库
from openpyxl import load_workbook

class DoExcel():
    def __init__(self,file,sheet):
        self.file=file
        self.sheet=sheet

    def get_excel_data(self):
        wb = load_workbook(self.file)#打开excel
        sheet_content = wb[self.sheet]#定位sheet工作博
        data_list = []#列表用于存储测试数据
        for n in range(2,sheet_content.max_row+1):#行,第一行是标题,所以从第二行开始
            data_dict = {}#字典用于存储每组测试数据
            for m in range(1,sheet_content.max_column+1):
                data_dict["method"]=sheet_content.cell(n,1).value
                data_dict["url"] = sheet_content.cell(n, 2).value
                data_dict["data"] = eval(sheet_content.cell(n, 3).value)#eval()将数据类型还原
                data_dict["expect"] = sheet_content.cell(n, 4).value
            data_list.append(data_dict)#将字典存储到list
        return data_list

if __name__ == '__main__':
    data_list = DoExcel("C:\\Users\\Administrator\\Desktop\\testdemo.xlsx","s1").get_excel_data()
    print(data_list)
#输出:[{'method': 'post', 'url': 'http://www.qabujiaban.com/user/login', 'data': {'username': 'uuuu222都44', 'password': 'WJHasb124*1'}, 'expect': '0000'}, {'method': 'get', 'url': 'http://www.qabujiaban.com/user/login', 'data': {'username': 'uuuu222都44', 'password': 'WJHasb124*1'}, 'expect': '0000'}]

 

标签:sheet,openpyxl,Python,list,Excel,value,cell,data
From: https://www.cnblogs.com/QAbujiaban/p/16994805.html

相关文章

  • Python 使用 smtp ssl 模式 发送邮件与附件
     参考:    发送邮件简单入门(以qq邮箱,163邮箱为    Python3使用SMTP发送带附件邮件:​​​https://www.jb51.net/article/142231.htm​​还可以使......
  • 安装 Python 包时解决 Microsoft visual c++ 14.0 is required 问题
     参考:​​http://www.hongweipeng.com/index.php/archives/1532/​​ 在windows上安装scrapy时经常会遇到这个问题,安装其他组件也可能会遇到。但问题解决办法都是大致......
  • Python PyQt5 教程
     PyQt5教程 :http://code.py40.com/face 教程翻译自:​​http://zetcode.com/gui/pyqt5/​​ 【第一节】PyQt5简介:​​http://code.py40.com/1948.html​​​【第二节】P......
  • Python GUI 开发工具
     PythonTkinter教程(GUI图形界面开发教程):​​http://c.biancheng.net/python/tkinter/​​PyQt5教程:​​http://code.py40.com/face​​ 作为 Pyhon 开发者,我们迟早都会......
  • Python 异步 IO 、协程、asyncio、async/await、aiohttp
    From:廖雪峰 异步IO:​​https://www.liaoxuefeng.com/wiki/1016959663602400/1017959540289152​​PythonAsync/Await入门指南:​​https://zhuanlan.zhihu.com/p/27258......
  • Python 大规模异步新闻爬虫、google、百度、有道、百度指数
    参考:​​https://www.yuanrenxue.com/crawler/news-crawler-urlpool.html​​url_pool.py#-*-coding:utf-8-*-#@Author:佛祖保佑,永无bug#@Date:#@File......
  • python 之将xmind转为excel用例文件
    1.xmind文件模板如下所示(最后一个子级为预置条件)2.excel用例模板3.获取xmind文件数据并转成字典形式fromxmindparserimportxmind_to_dict#xmind_to_dict可读取......
  • python+excel=openpyxl(一)
     原计划写一个openpyxl的操作文档,普及下python如何来操作excel,结果人家官方的文档已经写的非常完美了,就临时改主意把人家的文档翻译了一遍。可以阅读英文文档的同学,建议......
  • Python学习笔记--元组+字符串
    元组元组一旦定义完成,就不能再被修改同样,元组也可以进行嵌套操作当然,若是在元组里面嵌套一个list,那么list里面的元素是可以进行修改的!案例:实现:字符串查找索......
  • python-FunctionType动态创建函数,并改变函数名称
    方法一但无法编写foo函数体里面内容importtypesdeffoo(x,y):#print(x,y)return1x=1y=2f=types.FunctionType(foo.__code__,{},name='te......