我需要在 Excel 工作表中搜索包含某种模式的单元格。这需要的时间比我能处理的要多。我能写的最优化的代码如下。由于数据模式通常是一行一行的,所以我使用 iter_rows(row_offset=x)。不幸的是,下面的代码在每个 for 循环中发现给定模式的次数越来越多(从几毫秒开始到几乎一分钟)。我究竟做错了什么?
import openpyxl
import datetime
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.title = "test_sheet"
print("Generating quite big excel file")
for i in range(1,10000):
for j in range(1,20):
ws.cell(row = i, column = j).value = "Cell[{},{}]".format(i,j)
print("Saving test excel file")
wb.save('test.xlsx')
def FindXlCell(search_str, last_r):
t = datetime.datetime.utcnow()
for row in ws.iter_rows(row_offset=last_r):
for cell in row:
if (search_str == cell.value):
print(search_str, last_r, cell.row, datetime.datetime.utcnow() - t)
last_r = cell.row
return last_r
print("record not …Run Code Online (Sandbox Code Playgroud)