Skip to content

Commit 9e11bfd

Browse files
authored
Merge pull request #2314 from dvbridges/xlsmRead
BF: Allow .xlsm files to be read using importConditions()
2 parents 38412bf + d4fa018 commit 9e11bfd

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

psychopy/data/utils.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def pandasToDictList(dataframe):
270270
trialList.append(thisTrial)
271271
return trialList, fieldNames
272272

273-
if fileName.endswith('.csv') or (fileName.endswith(('.xlsx','.xls'))
273+
if fileName.endswith('.csv') or (fileName.endswith(('.xlsx','.xls','.xlsm'))
274274
and haveXlrd):
275275
if fileName.endswith('.csv'):
276276
trialsArr = pd.read_csv(fileName, encoding='utf-8-sig')
@@ -284,7 +284,7 @@ def pandasToDictList(dataframe):
284284
logging.debug(u"Clearing unnamed columns from {}".format(fileName))
285285
trialList, fieldNames = pandasToDictList(trialsArr)
286286

287-
elif fileName.endswith('.xlsx'):
287+
elif fileName.endswith(('.xlsx','.xlsm')):
288288
if not haveOpenpyxl:
289289
raise ImportError('openpyxl or xlrd is required for loading excel '
290290
'files, but neither was found.')
@@ -309,7 +309,11 @@ def pandasToDictList(dataframe):
309309
# get parameter names from the first row header
310310
fieldNames = []
311311
for colN in range(nCols):
312-
fieldName = ws.cell(_getExcelCellName(col=colN, row=0)).value
312+
if parse_version(openpyxl.__version__) < parse_version('2.0'):
313+
fieldName = ws.cell(_getExcelCellName(col=colN, row=0)).value
314+
else:
315+
# From 2.0, cells are referenced with 1-indexing: A1 == cell(row=1, column=1)
316+
fieldName = ws.cell(row=1, column=colN + 1).value
313317
fieldNames.append(fieldName)
314318
_assertValidVarNames(fieldNames, fileName)
315319

@@ -318,7 +322,11 @@ def pandasToDictList(dataframe):
318322
for rowN in range(1, nRows): # skip header first row
319323
thisTrial = {}
320324
for colN in range(nCols):
321-
val = ws.cell(_getExcelCellName(col=colN, row=rowN)).value
325+
if parse_version(openpyxl.__version__) < parse_version('2.0'):
326+
val = ws.cell(_getExcelCellName(col=colN, row=0)).value
327+
else:
328+
# From 2.0, cells are referenced with 1-indexing: A1 == cell(row=1, column=1)
329+
val = ws.cell(row=rowN + 1, column=colN + 1).value
322330
# if it looks like a list or tuple, convert it
323331
if (isinstance(val, basestring) and
324332
(val.startswith('[') and val.endswith(']') or

0 commit comments

Comments
 (0)