python openyxl 用法 教程

Python自动化办公:openpyxl教程(基础)-优快云博客

https://zhuanlan.zhihu.com/p/342422919

https://openpyxl-chinese-docs.readthedocs.io/zh-cn/latest/tutorial.html

列标题,是这一列 对应的单元格的格式,默认是常规,设置之后,对已有的数据双击单元格才会生效,没有数据的写入后自动生效。没有数据就是None,文本是以字符串存储

# 导入openpyxl模块的 load_workbook类
import datetime

from openpyxl import load_workbook

wb = load_workbook('实例.xlsx')
ws = wb[wb.sheetnames[0]]

# 读取表格数据
for row in ws.rows:
    print(row)
    for cell in row:
        print(type(cell.value),cell.value)

# 关闭文件
wb.close()
"""
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>, <Cell 'Sheet1'.D1>, <Cell 'Sheet1'.E1>, <Cell 'Sheet1'.F1>, <Cell 'Sheet1'.G1>)
<class 'str'> 常规
<class 'str'> 数值
<class 'str'> 文本
<class 'str'> 日期
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>, <Cell 'Sheet1'.D2>, <Cell 'Sheet1'.E2>, <Cell 'Sheet1'.F2>, <Cell 'Sheet1'.G2>)
<class 'float'> 1.32
<class 'float'> 1.32
<class 'str'> 1.32
<class 'datetime.datetime'> 2024-12-30 01:00:00
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>, <Cell 'Sheet1'.D3>, <Cell 'Sheet1'.E3>, <Cell 'Sheet1'.F3>, <Cell 'Sheet1'.G3>)
<class 'int'> 15689
<class 'int'> 15689
<class 'str'> 15689
<class 'datetime.datetime'> 2024-12-28 01:00:00
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
(<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>, <Cell 'Sheet1'.D4>, <Cell 'Sheet1'.E4>, <Cell 'Sheet1'.F4>, <Cell 'Sheet1'.G4>)
<class 'NoneType'> None
<class 'float'> 15689.01
<class 'str'> 123
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
(<Cell 'Sheet1'.A5>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.C5>, <Cell 'Sheet1'.D5>, <Cell 'Sheet1'.E5>, <Cell 'Sheet1'.F5>, <Cell 'Sheet1'.G5>)
<class 'NoneType'> None
<class 'int'> 158
<class 'str'> 1.56
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
(<Cell 'Sheet1'.A6>, <Cell 'Sheet1'.B6>, <Cell 'Sheet1'.C6>, <Cell 'Sheet1'.D6>, <Cell 'Sheet1'.E6>, <Cell 'Sheet1'.F6>, <Cell 'Sheet1'.G6>)
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
(<Cell 'Sheet1'.A7>, <Cell 'Sheet1'.B7>, <Cell 'Sheet1'.C7>, <Cell 'Sheet1'.D7>, <Cell 'Sheet1'.E7>, <Cell 'Sheet1'.F7>, <Cell 'Sheet1'.G7>)
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
(<Cell 'Sheet1'.A8>, <Cell 'Sheet1'.B8>, <Cell 'Sheet1'.C8>, <Cell 'Sheet1'.D8>, <Cell 'Sheet1'.E8>, <Cell 'Sheet1'.F8>, <Cell 'Sheet1'.G8>)
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
<class 'NoneType'> None
"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值