python - 打开受保护的 excel 文件太慢
问题描述
我开发了一个脚本,它使用 xlwings 打开大量受保护(不受密码保护,只是受保护)的 excel 文件,读取每个文件中的一列并将其写入字典,然后关闭。它可以正常工作而不会产生任何错误,但是速度非常慢。有没有比我下面的代码更快的方法?
import xlwings as xw
def unprotect_xls(filename, date):
workbook = xw.Book(filename)
sheet = workbook.sheets['Table1']
error_length[date] = dict(zip(range(1,21), sheet['BN7:BN26'].value))
workbook.close()
#### not working example ####
file_names = ['file1', 'file2', ..., 'file999']
dates = ['date1', ...]
new_files = len(file_names)
# make dict
error_length = {}
# open excel in background
app = xw.App(visible=False)
#fill dict
for i in range(new_files):
unprotect_xls(file_names[i], dates[i])
app.quit()
解决方案
您是否考虑过使用openpyxl
或xlrd
执行此任务?您还可以使用pandas.read_excel
在后台使用这些软件包之一的功能。xlwings
如果您需要与 Excel 进行交互,那就太好了,但如果您只需要批量读取单元格值,那么阅读器库可能更适合。
推荐阅读
- windows - Windows 上的 Cocoapods UTF-8 错误中的无效字节序列
- hyperledger-fabric - Hyperledger Fabric(链码实例化)错误:实例化策略违规:签名集不满足策略
- android - 如何将 Google Auth User 转换为 Phone Auth Firebase Android?
- kubernetes - 将 https 与从 IHS 到 K8S 的 HCL Connections Component Pack 6.5 一起使用
- javascript - 使用 keith wood 倒数计时器在 django 中进行实时倒计时
- php - Laravel - 外键向数据库添加数据数组而不仅仅是 id
- image - 在 Flutter 中重新加载图像帧
- ruby-on-rails - Rails,在 /books/:slug 之类的路线中,:to => 'books#show', slug: /.*?/ slug: /.*?/ 在做什么?
- python - 如何将 Web Scraping 代码变成循环?
- django - Django,Materialize,Django-tables2:预期的表或查询集,而不是 str