我正在使用带Python的xlrd库,通过UNC网络共享读取大约200个小型Excel文件。打开Excel文件的过程非常缓慢。我做了一个简单的基准测试程序来测量只需打开Excel文件所需的时间,而无需实际执行任何其他操作。
计时器报告需要92秒才能读取每个大小为50KB的100个Excel文件。为什么这需要这么长时间才能运行?通过访问相同网络共享的无线连接,大约需要2-3分钟!
最终,我需要从Excel工作簿中保存一些数据,然后使用收集的信息编写一个新的Excel文件。我做错了什么会减慢这个速度?
import time
import sys
import os
import re
from xlrd import open_workbook,cellname, XLRDError
from xlwt import Workbook
from timeit import default_timer as timer
def ReadReports():
start = timer()
filename_pattern = re.compile('^Report_name_.*')
count =0
root_directory = r'//network/path/somedirectory'
for path, dirs, files in os.walk(root_directory):
files[:] = [f for f in files if re.match(filename_pattern,f)]
for file in files:
count+=1
try:
#get the input file object (open the excel workbook)
fullpath = os.path.join(path,file)
book = open_workbook(fullpath)
except IOError:
logger.exception('Program encountered IOError')
continue
except XLRDError as e:
logger.exception('Program encountered XLRDError')
continue
else:
pass
print "num files: ", count
end = timer()
print "time: %r seconds" %(end - start)
if __name__ == "__main__":
ReadReports()
答案 0 :(得分:0)
我遇到同样的问题,这里有一些你可以尝试的事情: