Python3使用openpyxl读写Excel文件
Python中常用的操作Excel的三方包有xlrd,xlwt和openpyxl等,xlrd支持读取.xls和.xlsx格式的Excel文件,只支持读取,不支持写入。xlwt只支持写入.xls格式的文件,不支持读取。
openpyxl不支持.xls格式,但是支持.xlsx格式的读取写入,并且支持写入公式等。
原始数据文件apis.xlsx内容:
| name | method | url | data | json | result |
|---|---|---|---|---|---|
| get接口 | get | https://httpbin.org/get?a=1&b=2 | |||
| post表单接口 | post | https://httpbin.org/post | {name: Kevin,age:1} | ||
| post-json接口 | post | https://httpbin.org/post | {name: Kevin,age: 21} |
读取数据
读取所有数据
import openpyxl
# 打开excel
excel = openpyxl.load_workbook('apis.xlsx') # 有路径应带上路径
# 使用指定工作表
sheet = excel.active # 当前激活的工作表
# sheet = excel.get_sheet_by_name('Sheet1')
# 读取所有数据
print(list(sheet.values)) # sheet.values 生成器
print(sheet.max_column) # 最大列数
print(sheet.max_row) # 最大行数
显示结果:
[('name', 'method', 'url', 'headers', 'data', 'json', 'result'), ('get接口', 'get', 'https://httpbin.org/get?a=1&b=2', None, None, None, None), ('post表单接口', 'post', 'https://httpbin.org/post', 'cookie: token=123', '{name: Kevin,age: 21}', None, None), ('post-json接口', 'post', 'https://httpbin.org/post', None, None, '{name: Kevin,age: 21}', None)]
7
4
按行读取
代码接上例
...
# 按行读取
for row in sheet.iter_rows(min_row=1, min_col=1, max_col=3, max_row=3):
print(row)
# 读取标题行
for row in sheet.iter_rows(max_row=1):
title_row = [cell.value for cell in row]
print(title_row)
# 读取标题行以外数据
for row in sheet.iter_rows(min_row=2):
row_data = [cell.value for cell in row]
print(row_data)
打印结果:
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>)
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>)
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>)
['name', 'method', 'url', 'headers', 'data', 'json', 'result']
['get接口', 'get', 'https://httpbin.org/get?a=1&b=2', None, None, None, None]
['post表单接口', 'post', 'https://httpbin.org/post', 'cookie: token=123', '{name: Kevin,age: 21}', None, None]
['post-json接口', 'post', 'https://httpbin.org/post', None, None, '{name: Kevin,age: 21}', None]
读取单元格数据
代码接上例
...
# 读取单元格数据
print(sheet['A1'].value)
print(sheet.cell(1,1).value) # 索引从1开始
打印结果:
name
name
写入文件
代码接上例
# 写入单元格
sheet['F2'] = 'PASS'
result_col = title_row.index('result')+1 # 'result'所在的列号
sheet.cell(3, result_col).value = 'PASS'
# 整行写入
new_row = ['post-xml接口', 'post', 'https://httpbin.org/post']
sheet.append(new_row)
# 保存文件,也可覆盖原文件
excel.save("apis2.xlsx")
写入结果:
| name | method | url | data | json | result |
|---|---|---|---|---|---|
| get接口 | get | https://httpbin.org/get?a=1&b=2 | PASS | ||
| post表单接口 | post | https://httpbin.org/post | {name: Kevin,age:1} | PASS | |
| post-json接口 | post | https://httpbin.org/post | {name: Kevin,age: 21} | ||
| post-xml接口 | post | https://httpbin.org/post |
更多操作可参考官方文档: https://openpyxl.readthedocs.io/en/stable/
Python3使用openpyxl读写Excel文件的更多相关文章
- Python使用openpyxl读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- 【转发】Python使用openpyxl读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- openpyxl读写Excel文件
安装 pip install openpyxl 一个简单的实例: 最初的表格 #!/usr/bin/env python # -*- coding:utf-8 -*- import openpyxl ...
- Python使用读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- 用Python读写Excel文件(转)
原文:google.com/ncr 虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TA ...
- [转]用Python读写Excel文件
[转]用Python读写Excel文件 转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...
- python读写Excel文件的函数--使用xlrd/xlwt
python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket The ...
- 用Python读写Excel文件的方式比较
虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Ex ...
- MFC vs2012 Office2013 读写excel文件
近期在忙一个小项目(和同学一起搞的),在这里客户要求不但读写txt,而且可以读写excel文件,这里本以为很简单,结果...废话少说,过程如下: 笔者环境:win7 64+VS2012+Office2 ...
随机推荐
- Java数据结构HashMap
java数据结构HashMap /** * <html> * <body> * <P> Copyright JasonInternational</p> ...
- koa2中间件学习笔记
洋葱模型 整个洋葱就是服务端程序app,每层洋葱皮都是一个中间件,传入requrest,经过各个中间件处理之后传出response. 新建中间件m1.js,m2.js koa-learn/middle ...
- Docker启动Elasticsearch报错java.nio.file.AccessDeniedException
报错信息 Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes 问题分析 表面上是说容 ...
- nRF24L01无线介绍
CE:RX或TX模式选择 CSN:SPI片选信号 SCK:SPI时钟 MOSI:SPI数据输入 MISO:SPI数据输出 IRQ:可屏蔽中断脚 51测试程序 实测可用! #define TX_ADR_ ...
- computed和watch的使用场景
转载地址:https://blog.csdn.net/yuwenshi12/article/details/78561372 从作用机制和性质上看待methods,watch和computed的关系 ...
- 【leetcode】610. Triangle Judgement
原题 A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. ...
- Mysql中的锁机制-转载
原文:http://blog.csdn.net/soonfly/article/details/70238902 锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的 计算资源(如 ...
- Educational Codeforces Round 76 (Rated for Div. 2) D
D题 原题链接 题意:就是给你n个怪兽有一个属性(攻击力),m个英雄,每个英雄有两种属性(分别为攻击力,和可攻击次数),当安排最好的情况下,最少的天数(每选择一个英雄出战就是一天) 思路:因为怪兽是不 ...
- 实战 | 源码入门之Faster RCNN
前言 学习深度学习和计算机视觉,特别是目标检测方向的学习者,一定听说过Faster Rcnn:在目标检测领域,Faster Rcnn表现出了极强的生命力,被大量的学习者学习,研究和工程应用.网上有很多 ...
- 2018/7/31-zznuoj-问题 A: A + B 普拉斯【二维字符串+暴力模拟+考虑瑕疵的题意-0的特例】
问题 A: A + B 普拉斯 在计算机中,数字是通过像01像素矩阵来显示的,最终的显示效果如下: 现在我们用01来构成这些数字 当宝儿姐输入A + B 时(log10(A)<50,log10 ...