I have a spreadsheet with cells containing text with no formulas, those with formulas, and those formatted as datetime
. I need to read the actual value for all these three cases.
Using Python 3 with Openpyxl 3.0.3.
Currently, I have the following code. Using data_only=True
, as suggested on other SO posts about this kind of problem.
workbook = load_workbook(r"path\to\my\spreadsheet.xlsx", data_only=True)
# select first available sheet
sheet = workbook.active
sheet.title
for value in sheet.iter_rows(min_row=2,
max_row=86,
min_col=1,
max_col=7,
values_only=True):
print(value)
A snippet of the resulting output is shown below. In this example, the first column is fine. The third column is date-formatted, but I need the actual value of the cell (so they would be 26/1/2020 and 27/1/2020). The fifth and sixth columns have formulas, but I need the actual value.
(2, None, datetime.datetime(2020, 1, 26, 0, 0), None, '=SUM(D$3:D4)', '=E4/E3', None)
(3, None, datetime.datetime(2020, 1, 27, 0, 0), None, '=SUM(D$3:D5)', '=E5/E4', None)