Disclaimer: I have no idea how to work openpyxl. However, you mostly only need to worry about the datetime
module.
If you know which rows are supposed to be numbers, you could try code like this to convert an Excel date-format to a float, and ignore it if it's a number:
import datetime
import openpyxl
from openpyxl import load_workbook
# Source workbook - wb
wb = load_workbook(filename = r'C:\data\TEST.xlsx' , use_iterators=True)
ws = wb.get_sheet_by_name(name='QuoteFile ')
If val's a number, return it. Otherwise, take the difference between the datetime
and 1899-12-31 00:00:00. The way the datetimes work is they're internally a float,
being the number of days since the start of 1900. We get the number of seconds in
the delta (done through subtraction) and divide that by 86400 (the number of seconds
in a day).
def forcefloat(val):
"""If val's a number, return it. Otherwise, take the difference between the
datetime and 1899-12-31 00:00:00. The way the datetimes work is they're
internally a float, being the number of days since the start of 1900.
We get the number of seconds in the delta (done through subtraction)
and divide that by 86400 (the number of seconds in a day)."""
if isinstance(val, (int, float)):
return val
assert isinstance(val, datetime.datetime)
return (val - datetime.datetime(1899,12,31,0,0,0)).total_seconds() / 86400
for row in ws.iter_rows():
print(
row[0].internal_value,
forcefloat(row[3].internal_value),
row[4].internal_value,
row[5].internal_value,
)
print('Done')
Not exactly the most elegant solution, but it works.