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开始

打印结果:

Copy
name
name

写入文件

代码接上例

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:725638078
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
# 写入单元格
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

Python基础入门:使用openpyxl读写Excel文件相关推荐

  1. Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 Python使用openpyxl读取excel文件中数据 Python使用openpyxl往excel文件中写入数据 Python使用openpyx ...

  2. Python使用xlwt和xlrd读写excel文件

    Python使用xlwt和xlrd读写excel文件 xlwt和xlrd是两个相互配套的模块,在Python中,用于将数据写入Excel文件和读取Excel文件的数据. 从字面即可看出xlwt是对xl ...

  3. python基础教程 excel_python基础教程 excel-python怎么读写excel文件

    excel和python哪个难学 excel 分应用和VBA编程,phthon本就是编程语言,对数据分析来说,excel足够了,而且是最容易学习的数据处理与分析应用软件,Python肯定比excel难 ...

  4. python怎么读取excel-python如何读写excel文件

    python读写excel的方式有很多,不同的模块在读写的方法上稍有区别: 用xlrd和xlwt进行excel读写: 用openpyxl进行excel读写: 用pandas进行excel读写: 为了方 ...

  5. python怎么读excel_python怎么读写excel文件

    展开全部 最近用python处理一个小项目32313133353236313431303231363533e4b893e5b19e31333363393732,其中涉及到对excel的读写操作,通过查 ...

  6. python openpyxl读取excel_Python使用openpyxl读写excel文件

    这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 1.读取Excel文件 默认打开的文件为可读写,若有需要可 ...

  7. python怎么读excelsheet_python怎么读写excel文件

    python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库.下面记录python读取excel. python读excel--xlrd 这个过程 ...

  8. python打印excel_Python利用openpyxl处理Excel文件(打印机及页面设置相关)

    对于打印机及页面设置,使用openpyxl来处理个人觉得意义不是很大.不作为此次主题的重点来讲解,只作简单的梳理回顾. 一.编辑打印选项(居中方式) >>> from openpyx ...

  9. Python读写Excel文件

    python如何读写excel文件 python读写excel的方式有很多,不同的模块在读写的方法上稍有区别: 用xlrd和xlwt进行excel读写: 用openpyxl进行excel读写: 用pa ...

最新文章

  1. 看完这篇Exception 和 Error,和面试官扯皮就没问题了
  2. 什么叫单模光纤_什么叫单模光纤_单模光纤的特点是什么
  3. springboot打Jar包和War包
  4. 山西财经大学计算机考试题及答案,2018年山西财经大学计算机应用技术408计算机学科专业基础综合之计算机操作系统考研基础五套测试题...
  5. Hystrix解决雪崩问题的两种手段
  6. HTTP over QUIC重命名为“HTTP / 3”协议
  7. Micropython 如何用Turnipbit做一个自动浇水装置
  8. python使用全局变量的坑,要使用global
  9. android 字符串 时间格式化,Android 获取年月日时分秒 格式化指定时间字符串
  10. 开发者说丨如何从零开始构建一个轻量级应用
  11. OpenShift Security 15 - 用 RHACS 的安全策略管理运行中的容器安全
  12. B站上的github视频教程笔记(包含两个B站视频,我觉得看这两个B站视频,github从原理到操作都可以会了)
  13. 最实用的正则表达式整理
  14. Busybox中httpd、ftpd、telnetd、tftpd、ntpd的用法
  15. python的parse函数没有执行——问题已解决
  16. peoplesoft 更新表接口程序
  17. # 无法打开注册表项 UNKNOWN\Components\ #
  18. java/php/net/python守望先锋网站设计
  19. Win11系统时间不同步的解决方法
  20. 整体看看Android的多媒体系统(多图)

热门文章

  1. iOS自定义转场动画实战讲解
  2. @interface CTDataFactoryViewController : CTMessageViewController
  3. 绝不是剧透!全角度解析EMC Unity绝妙重头戏
  4. 第一章 基础设施,1.3 阿里视频云ApsaraVideo是怎样让4000万人同时狂欢的(作者:蔡华)...
  5. PowerShell 收集计算机相关信息
  6. 原生态纯JavaScript 100大技巧大收集---你值得拥有
  7. 老公,等儿子长大,我去天堂找你(推荐)
  8. mysql 同步中历史记录_[Mysql]备份同库中一张表的历史记录 insert into ..select
  9. 计算机在课堂中的应用论文,任务教学法在计算机教学中的应用论文
  10. python整数类型进制表示_python2学习经验(一) 变量数据类型