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 ...
随机推荐
- Map 集合遍历的4种方法
Map 集合初始化时,指定集合初始值大小. 说明:HashMap 使用 HashMap(int initialCapacity) 初始化. 正例:initialCapacity = (需要存储的元素个 ...
- iis7 运行多个https,433端口监听多个htps 站点
默认情况一个服务器的IIS只能绑定一个HTTPS也就是443端口,现在有需要一个服务器 iis 433 端口 绑定多个 申请到证书后(不是必须要通配符的证书),添加多个https站点,先绑定别的端口 ...
- flex应用实例
代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- js大数计算之展示
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- redis slot 槽点
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求 ...
- go语言的局部变量在堆上还是栈上?
在讨论之前,先看如下代码: type treeNode struct { value int left, right *treeNode } func createNode(value int) *t ...
- MySQL中SQL语句常见优化策略
1.避免全表扫描 对查询进行优化,应尽量避免全表扫描,首先应考虑在where 及order by 涉及的列上建立索引. 2.避免判断null 值 应尽量避免在where 子句中对字段进行null 值判 ...
- libssh2--ssh2实例
#include "libssh2_config.h"#include<libssh2.h>#include<libssh2_sftp.h> 上述为所包含必 ...
- 8.vue-resource 数据请求基本实现
1.vue-resource 实现 get, post, jsonp请求:https://github.com/pagekit/vue-resource 注意: 除了 vue-resource 实现数 ...
- Python程序打包工具PyInstaller
Python程序执行 py文件:直接提供源码,需要使用者自行安装Python并且安装依赖的各种库 pyc文件:pyc文件是Python解释器可以识别的二进制码,是跨平台的,需要使用者安装相应版本的Py ...