-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathparallel_xlrd.py
28 lines (23 loc) · 1.06 KB
/
parallel_xlrd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import multiprocessing
from itertools import repeat
import xlrd
import excel
def _read_sheet(filename, sheetname):
# The leading underscore in the function name is used by convention
# to mark it as "private", i.e., it shouldn't be used directly outside
# of this module.
with xlrd.open_workbook(filename, on_demand=True) as book:
sheet = book.sheet_by_name(sheetname)
data = excel.read(sheet)
return sheet.name, data
def open_workbook(filename, sheetnames=None):
if sheetnames is None:
with xlrd.open_workbook(filename, on_demand=True) as book:
sheetnames = book.sheet_names()
with multiprocessing.Pool() as pool:
# By default, Pool spawns as many processes as there are CPU cores.
# starmap maps a tuple of arguments to a function. The zip expression
# produces a list with tuples of the following form:
# [('filename.xlsx', 'Sheet1'), ('filename.xlsx', 'Sheet2)]
data = pool.starmap(_read_sheet, zip(repeat(filename), sheetnames))
return {i[0]: i[1] for i in data}