EXCEL+PYTHON学习3

1) 遍历一个SHEET,无非就是两个循环,rows属性是取得所有行。

复制代码
fn = 'data3_16.xlsx'
wb = openpyxl.load_workbook(fn)
ws = wb.active
for row in ws.rows:
    for cell in row:
        print(cell.value, end=' ')
    print()
2) 返回工作表的最小行数和最小列数
  
复制代码
fn = 'data3_19_1.xlsx'
wb = openpyxl.load_workbook(fn)
ws = wb.active
print(f"工作表有资料最小行数 = {ws.min_row}")
print(f"工作表有资料最大行数 = {ws.max_row}")
print(f"工作表有资料最小列数 = {ws.min_column}")
print(f"工作表有资料最大列数 = {ws.max_column}")

3)用values_only=True,可以显示遍历每个行中的单元格内容:
比如EXCEL:

则:
复制代码
fn = 'data3_19_1.xlsx'
wb = openpyxl.load_workbook(fn)
ws = wb.active
for row in ws.iter_rows(values_only=True):
    print(type(row))
    print(row)
   输出:
<class 'tuple'>
(1, 5, 9, 13)
<class 'tuple'>
(2, 6, 10, 14)
<class 'tuple'>
(3, 7, 11, 15)
<class 'tuple'>
(4, 8, 12, 16)

4) 获得指定行或列的值:

复制代码
fn = 'data3_16.xlsx'
wb = openpyxl.load_workbook(fn)
ws = wb.active                               
for cell in ws['A']:    # A列
    print(cell.value)
for cell in ws[5]:      # 索引是5,输出第5行的所有内容。
    print(cell.value, end=' ')     
相关推荐
Elias不吃糖4 小时前
Java Lambda 表达式
java·开发语言·学习
梨子串桃子_4 小时前
推荐系统学习笔记 | PyTorch学习笔记
pytorch·笔记·python·学习·算法
jjjxxxhhh1234 小时前
spdlog介绍使用
学习
曾浩轩6 小时前
图灵完备Turing Complete 3
学习
天天睡大觉6 小时前
Python学习11
网络·python·学习
laplace01236 小时前
# 第六章 agent框架开发实践 - 学习笔记
人工智能·笔记·学习·语言模型·agent
wtsolutions6 小时前
JSON to Excel Add-in - Seamless Integration Within Excel
json·excel
wtsolutions6 小时前
Getting Started with JSON to Excel Web App - Convert in Seconds
json·excel·web app
坚持不懈的大白7 小时前
Leetcode学习笔记
笔记·学习·leetcode
SWAGGY..7 小时前
数据结构学习篇(10)--- 二叉树基础oj练习
数据结构·学习