Skip to content
Advertisement

Error while trying to get ReceivedTime from Win32com Python bib

I’m receiving an error while I’m trying to save a code that gets an outlook’s mail receivedTime. But I don’t know what possibly could done wrong. See the error below:

Exception has occurred: TypeError
Excel does not support timezones in datetimes. The tzinfo in the datetime/time object must be set to None.

Can anyone help me?

The code:

from datetime import datetime
from operator import index
from typing import Pattern
import win32com.client
import openpyxl
from openpyxl import Workbook
from openpyxl.styles import Font, Color, PatternFill, fills, Border, Side


outlook = win32com.client.Dispatch("outlook.application")
mapi = outlook.GetNamespace("MAPI")
account = [i for i in mapi.Accounts][0]
inbox = mapi.GetDefaultFolder(6)
test_folder = inbox.Folders["TravelAlertReport"]
items = test_folder.Items

wb= Workbook()
planilha = wb.worksheets[0]
ws=wb["Sheet"]

#Header

list_header = ["Status", "Level", "Location", "Category", "Date", "Month", "Year", "Relevant?(Yes/No)", "Justification", "Used by TST", "Learning Required?"]

#Rows' content    
for index,item in enumerate(items):
        
#Time
        date_time = item.ReceivedTime
        ws.cell(row=index+2, column = 5).value = date_time

wb.save("Report.xlsx")

Advertisement

Answer

The MailItem.ReceivedTime property returns a Date indicating the date and time at which the item was received.

Try to use any other date object in the code to see whether any difference exists, for example:

'Insert Today's Date
Range("A1").Value = Date
  
'Insert A Date (mm/dd/yyyy)
Range("B1") = #1/28/2019#

If that code works then you need to format the date object in the way which Excel understands. The Format function can help with that.

Advertisement