Python计算Outlook邮件项目的老化

时间:2016-08-28 09:14:39

标签: python email

我正在尝试在wxPython中开发一个应用程序,在其中我将所有电子邮件提取到listctrl中,并根据剩余的SLA(3天)对它们进行排序以进行进一步操作。为此,我将通过从当前时间减去receivedTime来计算邮箱中项目的老化。以下是我的完整代码: -

from datetime import datetime
import time
import win32com.client
import wx

class MainFrame(wx.Frame):
    def __init__(self, parent, myTitle):
        super(MainFrame, self).__init__(parent, title = myTitle, size = (1300,600))
    #set the background color
    self.index = 0
    self.SetBackgroundColour((230, 230, 250))

    #create fonts
    FrameLabels_Font = wx.Font(10, wx.DECORATIVE, wx.NORMAL, wx.BOLD, True, u'Bookman Old Style')
    listItems_Font = wx.Font(8, wx.DECORATIVE, wx.NORMAL, wx.NORMAL, True, u'Bookman Old Style')

    #set the frame
    self.frame_Controls = wx.StaticBox(self, label="Controls:-", pos=(10, 275), size=(300, 250), style=wx.RAISED_BORDER)
    self.frame_Controls.SetBackgroundColour((230, 230, 250))
    self.frame_Controls.SetFont(FrameLabels_Font)
    self.frame_Summary = wx.StaticBox(self, label="Stats:-", pos=(10, 10), size=(300, 250), style=wx.RAISED_BORDER)
    self.frame_Summary.SetBackgroundColour((230, 230, 250))
    self.frame_Summary.SetFont(FrameLabels_Font)
    self.frame_Queue = wx.StaticBox(self, label="Work Items:-", pos=(320, 10), size=(950, 515), style=wx.RAISED_BORDER)
    self.frame_Queue.SetBackgroundColour((230, 230, 250))
    self.frame_Queue.SetFont(FrameLabels_Font)

    #controls for queue frame
    LblRegion = wx.StaticText(self.frame_Queue, -1, 'Region:-', (20, 25), (150, 25))
    LblMailbox = wx.StaticText(self.frame_Queue, -1, 'Mailbox:-', (190, 25), (150, 25))
    LblSortBy = wx.StaticText(self.frame_Queue, -1, 'Sort By:-', (360, 25), (150, 25))
    LblSortOrder = wx.StaticText(self.frame_Queue, -1, 'Sort Order:-', (530, 25), (150, 25))

    rdoUnallocated = myRadioButton(self.frame_Queue, 'UnAllocated', (700,25), (110,25))
    rdoAllocated = myRadioButton(self.frame_Queue, 'Allocated', (820, 25), (110, 25))

    rgnCombo = myComboBox(self.frame_Queue,(20,60),(150,25))
    rgnCombo = myComboBox(self.frame_Queue, (190, 60), (150, 25))
    rgnCombo = myComboBox(self.frame_Queue, (360, 60), (150, 25))
    rgnCombo = myComboBox(self.frame_Queue, (530, 60), (150, 25))

    self.subList = myListCtrl(self.frame_Queue,(20,95),(910,390))
    self.subList.InsertColumn(0, 'Rush')
    self.subList.InsertColumn(1, 'Subject')
    self.subList.InsertColumn(2, 'Recevd DtTm')
    self.subList.InsertColumn(3, 'Allocated To')
    self.subList.InsertColumn(4, 'Allo. ID')
    self.subList.InsertColumn(5, 'Unique Key')
    self.subList.InsertColumn(6, 'Rem. SLA')
    self.subList.InsertColumn(7, 'Ageing')
    self.subList.InsertColumn(8, 'Duplicate')
    self.subList.InsertColumn(9, 'Actionable')
    self.subList.InsertColumn(10, 'Status')
    self.subList.InsertColumn(11, 'Start DtTm')
    self.subList.InsertColumn(12, 'Query DtTm')
    self.subList.InsertColumn(13, 'Hold DtTm')
    self.subList.InsertColumn(14, 'Continue DtTm')
    self.subList.InsertColumn(15, 'Final Status')
    self.subList.InsertColumn(16, 'Final Status DtTm')
    self.subList.InsertColumn(17, 'Final Status Date')

    #update the listctrl
    getConn = OutlookConnection()
    messages = getConn.fetchUnallocated()
    for msg in messages:
        self.subList.InsertStringItem(self.index, '')
        self.subList.SetItemFont(self.index, listItems_Font)
        self.subList.SetStringItem(self.index, 1, msg.subject)
        self.subList.SetStringItem(self.index, 2, str(msg.receivedtime))
        if msg.Importance == 2:
            self.subList.SetStringItem(self.index, 0, 'Y')
            self.subList.SetItemBackgroundColour(self.index, (255,0,0))
        #tm = datetime.now().strftime("%m-%d-%Y %H:%M:%S") - msg.receivedtime
        #tm = datetime.now() - msg.receivedtime
        tm = time.mktime(datetime.now().timetuple()) - msg.receivedtime
        #tm = datetime.now() - datetime.fromtimestamp(time.mktime(msg.receivedtime))
        #self.subList.SetStringItem(self.index, 7, str(datetime.now().strftime("%m-%d-%Y %H:%M:%S")))
        self.subList.SetStringItem(self.index, 7, str(tm))

    #add the menu here
    self.AddMenu()

    #display the frame
    self.Centre()
    self.Show()

#create the AddMenu def
def AddMenu(self):
    menuBar = wx.MenuBar()
    #file menu
    File_btn = wx.Menu()
    #sub menu items of file menu
    #Logout
    Logout_btn = File_btn.Append(wx.ID_EXIT,'&Logout', 'Close the application')
    #now put the File_btn to the menuBar
    menuBar.Append(File_btn, '&File')
    #set the menu bar in the application main frame
    self.SetMenuBar(menuBar)
    #now bind the code which will run upon clicking the Logout button
    self.Bind(wx.EVT_MENU, self.Quit, Logout_btn)

#def the self.Quit process
def Quit(self,x):
    self.Close()

#class for querying database
class dbQuery():
#method for getting the list of regions
    def RegionsList(self):
        myDb = 'H:\\Python\\wxPython\\Programs\\References.accdb'
        DRV = '{Microsoft Access Driver (*.mdb)}'
        PWD = 'pw'
    # connect to db
        conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s' % (myDb))
        cur = conn.cursor()
    # run a query and get the results
        SQL = 'SELECT * FROM Regions'
        return cur.execute(SQL, self.Tname).fetchall()
        cur.close()
        conn.close()


class myRadioButton(wx.RadioButton):
    def __init__(self, parent, mylabel, vPosition, vSize):
        super(myRadioButton, self).__init__(parent, -1, label = mylabel, pos = vPosition, size = vSize)

class myComboBox(wx.ComboBox):
    def __init__(self, parent, lstposition, lstsize):
        super(myComboBox, self).__init__(parent, -1, value="", pos=lstposition, size=lstsize)
    #this method will be used to add items from a list to the instance of the mycombobox
    def addItem(self, Lst=[]):
        for itm in Lst:
            self.Append(itm)

class myListCtrl(wx.ListCtrl):
    def __init__(self,parent, vPosition, vSize):
        super(myListCtrl, self).__init__(parent, -1, pos = vPosition, size = vSize, style=wx.LC_REPORT
                         |wx.BORDER_SUNKEN)


class OutlookConnection():
    def fetchUnallocated(self):
        outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
        inbox = outlook.GetDefaultFolder(6)  # "6" refers to the index of a folder - in this case inbox,
        return inbox.Items


app = wx.App()
FIR_Frame = MainFrame(None, 'F.I.R - TL Interface')
app.MainLoop()

我面临的挑战是我试图计算邮件项目的老化: -

tm = datetime.now() - msg.receivedtime
self.subList.SetStringItem(self.index, 7, str(tm))

我收到错误: - tm = datetime.now() - msg.receivedtime TypeError:不支持的操作数类型 - :' datetime.datetime'和'时间'

我尝试了更多格式/方式...您可以在上面的代码中看到它们导致类似的错误

有人可以帮我计算老化吗?HH:MM:SS'格式。此外,如果需要,小时数应超过24小时(即如果老化超过24小时)。

提前非常感谢你。

此致 Premanshu

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点,这是一种方式 检查msg.receivedtime的结构是什么,然后将其转换为秒以便能够操作它。我假设msg.receivedtime“2016-08-30 10:30:15”的结构如下 使用time.time()而不是datetime.now()来获取now

>>> tm = int(time.time())
>>> tm
1472550462
>>> msg_receivedtime = "2016-08-30 10:30:15"
>>> msg_tuple = time.strptime(msg_receivedtime, "%Y-%m-%d %H:%M:%S")
>>> msg_stamp = int(time.mktime(msg_tuple))
>>> msg_stamp
1472545815
>>> print tm - msg_stamp
4647

注意:在两种情况下,我都将时间戳减少到整数秒 编辑:
将秒转换为hh:mm:ss

>>> x = 4647
>>> m,s =divmod(x,60)
>>> h,m =divmod(m,60)
>>> print h,m,s
1 17 27

编辑:关于超过48小时的日期戳的评论

你一定做错了!
以下是上面的代码,使用截至2016年9月5日星期一的time.time()值。

>>> import time
>>> tm = int(time.time())
>>> tm
1473064274
>>> msg_receivedtime = "2016-08-30 10:30:15"
>>> msg_tuple = time.strptime(msg_receivedtime, "%Y-%m-%d %H:%M:%S")
>>> msg_stamp = int(time.mktime(msg_tuple))
>>> msg_stamp
1472545815
>>> x = tm - msg_stamp
>>> x
518459
>>> m,s =divmod(x,60)
>>> h,m =divmod(m,60)
>>> print h,m,s
144 0 59

请注意,144小时为6天 这就是你所需要的:

  

有人可以帮我计算'HH:MM:SS'格式的老化。此外,如果需要,小时数应超过24小时(即如果老化超过24小时)。