0

I am trying to print the contents of my dictionary with actual time values (for example, '6:00 AM') from my workbook. I get a different time format when I print from 'TimeSheet' than I do 'From'. How can I get the actual time value to print.

enter image description here

import openpyxl

wb = openpyxl.load_workbook('Sample.xlsx')

sheet = wb.get_sheet_by_name('Sheet2')
for i in range(1, 57):
   From = sheet.cell(row=i, column=1).value
   To = sheet.cell(row=i, column=2).value
   Activity = sheet.cell(row=i, column=3).value
   TimeSheet = {'From': From, 'To': To, 'Activity': Activity}
   print(TimeSheet)

Current output:

{'Activity': 'ACTIVITY', 'From': 'FROM', 'To': 'TO'}
{'Activity': None, 'From': datetime.time(6, 0), 'To': datetime.time(6, 15)}
{'Activity': None, 'From': datetime.time(6, 15), 'To': datetime.time(6, 30)}
{'Activity': None, 'From': datetime.time(6, 30), 'To': datetime.time(6, 45)}
{'Activity': None, 'From': datetime.time(6, 45), 'To': datetime.time(7, 0)}
{'Activity': None, 'From': datetime.time(7, 0), 'To': datetime.time(7, 15)}
{'Activity': None, 'From': datetime.time(7, 15), 'To': datetime.time(7, 30)}
{'Activity': None, 'From': datetime.time(7, 30), 'To': datetime.time(7, 45)}
3

2 Answers 2

2

You're looking for strftime (string-format-time).

>>> from datetime import datetime

>>> datetime.now()
datetime.datetime(2016, 6, 14, 17, 24, 27, 735835)

>>> datetime.now().strftime("%Y %m %d")
'2016 06 14'

The python documentation on the subject is pretty extensive, and there's also this convenient reference table for the format language.

Sign up to request clarification or add additional context in comments.

Comments

0

As others already stated, you can use strftime to modify the formatting of your time, like so:

format = '%H:%M %p'  # Hour, Minute, AM or PM
TimeSheet = {'From': From.strftime(format), 'To': To.strftime(format), 'Activity': Activity}
# {'From': '6:00 AM', 'To': '6:15 AM', 'Activity': None}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.