Search code examples
pythonexcelpython-3.xopenpyxlxlrd

How to convert MS Excel numbers in general format (e.g 43422) to date in python?


After fetching data from Excel to Python, the dates have been fetched into python as '43422' format. How do I convert this integer to date format in python now?


Solution

  • You may be able to prevent this from happening in the first place when you read in the data: pd.read_excel('file.xlsx'). Make sure that the data is properly formatted as a date before you read in the file.

    If you want to convert this number to a date after you read it in, use the fact that it represents the number of days since January 1, 1900:

    import datetime
    
    d = '45422'
    
    date = datetime.date(1900, 1, 1) + datetime.timedelta(int(d))
    date
    

    Output:

    datetime.date(2024, 5, 12)