Home > Back-end >  how to read date in a specific format in openpyxl
how to read date in a specific format in openpyxl

Time:01-23

I have an excel column which is a date column with values like 1-JAN-22.

But when I read using openpyxl the value shows as 2022-01-01 00:00:00.

I'm extracting the value with value = sh[cell_name].value

How do I extract as I see in excel, 1-Jan-22? I just want to extract this string.

CodePudding user response:

If you are on Windows, try

value = datetime.date.strftime(sh['A1'].value, "%#d-%b-%y")

If on Linux, try

value = datetime.date.strftime(sh['A1'].value, "%-d-%b-%y")

Output:

'1-Jan-22'
  •  Tags:  
  • Related