Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I disable openpyxl from automatically parsing strings to datetime?

I'm having simple excel files with various timstamp formats written as strings.
There is a build in feature in openpyxl to automatically convert what seems like a date to a datetime object.

My question is simple, how can I take the raw string as it was inserted to the excel file by the user, without intervention of openpyxl.

I want to do my own format testings using a function that tries various calls to datetime.strptime myself.

Loading the excel is done by me like this:

import openpyxl
ex = openpyxl.load_workbook('/path/to/file.xls')
worksheet = ex.active

In case needing to iterate over rows I'm using worksheet.iter_rows method

like image 267
JavaSa Avatar asked Sep 11 '25 14:09

JavaSa


1 Answers

You're assumption is incorrect: openpyxl does not convert strings unless you ask it to by setting guess_types=True. Excel treats the values as datetime objects by setting the number format and internally converting them to serials with an epoch of 1899-12-30.

like image 144
Charlie Clark Avatar answered Sep 15 '25 04:09

Charlie Clark