Python Openpyxl: how to stop inference or override the cell data type

Viewed 131

I have an number of Excel workbooks which I need to extract the 'PeriodYear' from. This data is held in the 'Overview' worksheet in the cell 'H7'.

I've been recommended to use openpyxl to achieve this goal, however I'm having a problem with it trying to infer the cell data type based on the cell's value which is having some strange results.

from openpyxl import load_workbook

workbook = load_workbook(filename=<excel file name>, read_only=True, data_only=True)

sheet = workbook["Overview"]

print(sheet["H7"].value)

Some examples of cell value as viewable in the worksheet and the openpyxl output result below:

  • '20' (prints 20)
  • '2020' (prints 2020)
  • '02' (prints 2)
  • '19 / 20' (prints 0.95)

Is there a way to force openpyxl to stop inferring the data type of a cell, or to override it to read cell values as a string instead?

Or is there a different way of achieving the same goal not using openpyxl?

0 Answers
Related