apply_stylesheet slows down load_workbook in some cases (even in read_only mode)
Hello,
In my daily work I need to read thousands of pre-formatted excel sheets filled by external companies.
In some cases I've found that the load_workbook
function is very slow (even in read-only mode), when there are (unuseful) complex styles in the workbook (in one case it took 15s to open a relatively small excel file of about 400kB).
From cProfile I've spotted that the slowness is caused by the apply_stylesheet
function in the read
method of ExcelReader
. Given that in my case I do not need the styles but only the values, I added a further property (apply_styles
) to the ExcelReader
object and rewrote the read methos as:
def read(self):
self.read_manifest()
self.read_strings()
self.read_workbook()
self.read_properties()
self.read_theme()
if self.apply_styles:
apply_stylesheet(self.archive, self.wb)
self.read_worksheets()
self.parser.assign_names()
if not self.read_only:
self.archive.close()
Obviously I also edited the __init__
and load_workbook
functions accordingly.
In that way the reading was much faster. My script now reads about 400 excel files and put it in a single pandas dataframe in about 40s, while before it took more than 5 minutes.
This is a very simple thing but it can speed up the codes in many situations. I hope you will take it into account.
Best,
Matteo