Skip to content
Advertisement

Python openpyxl get date output format

i have an excel file that i cant change, where im getting some info with openpyxl, on this case i have a cell with this DATE format MM/DD/YYYY i need the output on python to be: %d/%m/%Y

Ive tried this :

JavaScript

I get this error :

JavaScript

How can i make this work correctly? (im new in python and also on programming)

Advertisement

Answer

datetime.strptime grabs a string and converts it into a datetime object. The format you give it tells it how to interpret the string it’s grabbing. In this case, you’re telling it that you’re giving it a string formatted like '%d/%m/%Y', when in fact it appears you’re giving it a string formatted like '%Y-%m-%d %H:%M:%S'. This is the format string you should give it. You will then have a datetime object representing that date/time. If you want to have a new string in the format you indicated, take that datetime object and use the .strftime method, passing your format string to it, and it will output the string how you want it:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement