当前位置:网站首页>【openpyxl】只读模式、只写模式
【openpyxl】只读模式、只写模式
2022-08-10 23:50:00 【冰冷的希望】
1.说明
前面我们使用的normal模式进行读写Excel文件,这是一种兼顾读写相对比较平衡的模式,但是,数据加载到内存占用的资源是比较大的,大概是文件的50倍,如果你的Excel文件本身就10M,加载之后程序
需要占用0.5G内存,这很不划算(大内存电脑请自动忽略),所以我们需要考虑是不是可以选择只读或只写模式以便提高性能
2.只读模式
只读模式,如果你需要读取很大的Excel文件,但是又不改变和保存,例如只读取数值用于其他数据分析,这时候我们完全可以使用只读模式提供性能
from openpyxl import load_workbook
# 加载Excel文件时使用read_only指定只读模式
wb = load_workbook(filename='large_file.xlsx', read_only=True)
ws = wb['big_data']
# 可以正常读取值
for row in ws.rows:
for cell in row:
print(cell.value)
# 注意:读取完之后需要手动关闭避免内存泄露
wb.close()
load_workbook参数说明:
定义:
def load_workbook(filename, read_only=False, keep_vba=KEEP_VBA, data_only=False, keep_links=True)
参数:
read_only:是否只读,默认False
keep_vba:是否使用VBA编程,默认False
data_only:是否只加载数据值,即丢弃公式、排序等操作,默认False
keep_links:是否保留超链接,默认True
3.只写模式
如果文件是以写为主,可以在创建工作簿的时候指定为只写模式以便提高性能,不管文件有多大,都可以把内存保持在10M以下
from openpyxl import Workbook
from openpyxl.cell import WriteOnlyCell
from openpyxl.comments import Comment
from openpyxl.styles import Font
wb = Workbook(write_only=True) # 创建工作簿时指定只写模式
ws = wb.create_sheet() # 需要通过create_sheet创建一个sheet
# 可以正常保存数据
for _ in range(100):
ws.append([i for i in range(200)]) # 只能通过append写
# 如果需要保留公式、注释等操作,可以使用WriteOnlyCell
cell = WriteOnlyCell(ws, value="冰冷的希望")
cell.font = Font(name='黑体', size=15)
cell.comment = Comment(text="这是注释", author="pan")
ws.append([cell])
wb.save('openpyxl/test.xlsx')
只写模式注意点:
1.需要通过create_sheet()创建表
2.只能通过append()增加数据,不能通过cell或iter_rows()
3.wb.save()之后不能再修改,否则抛出WorkbookAlreadySaved异常
边栏推荐
- [Excel知识技能] 将文本型数字转换为数值格式
- 【C语言】数据储存详解
- YOLOv5的Tricks | 【Trick10】从PyTorch Hub加载YOLOv5
- How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
- 盘点美军的无人机家底
- 【pypdf2】安装、读取和保存、访问页面、获取文本、读写元数据、加密解密
- [C language] First understanding of pointers
- LENS CRA和SENSOR CRA匹配问题解析
- Promise in detail
- 回收站的文件删了怎么恢复,回收站文件恢复的两种方法
猜你喜欢

14. Thymeleaf

地下管廊可视化管理系统搭建
![[C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)](/img/ae/74dc5fc676e74ab03607565ca7539e.png)
[C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)
![Which translation software is more accurate [Free]](/img/12/33d6724cfe8e8fe12a131c1e5e7a69.png)
Which translation software is more accurate [Free]

电脑桌面删除的文件回收站没有,电脑上桌面删除文件在回收站找不到怎么办

13. Content Negotiation

SQL injection base

服务器小常识
![[C language] binary search (half search)](/img/24/4e7b54963ac9df4a3244232c32c435.png)
[C language] binary search (half search)

Jvm.分析工具(jconsole,jvisualvm,arthas,jprofiler,mat)
随机推荐
Which foreign language journals and conferences can be submitted for software engineering/system software/programming language?
Lens filter---about day and night dual-pass filter
Easy-to-use translation plug-in - one-click automatic translation plug-in software
Server Tips
高校就业管理系统设计与实现
Where can I download IEEE papers?
Qt入门(六)——抽奖系统的实现
图像识别和语义分割的区别
如何快速把握行业机会,更高效地推陈出新,是一个重要的命题
C语言%(%d,%c...)
What is the ASIO4ALL
ROS Experimental Notes - Install QPEP and Intel-MKL
图片懒加载(纯手写)
9. Rest 风格请求处理
【.NET Core】使用 NPOI 读写Excel 文件
【C语言篇】操作符之 位运算符详解(“ << ”,“ >> ”,“ & ”,“ | ”,“ ^ ”,“ ~ ”)
CF1427F-Boring Card Game【贪心】
web 性能提升(将持续更新……)
Part of the reserve bank is out of date
Software protection scenario of NOR FLASH flash memory chip ID application