Python计算Outlook邮件项目的老化 [英] Python calculate ageing of outlook mail items
问题描述
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))
#设置背景颜色
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')
#设置框架
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,labe l =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为队列帧
LblRegion = wx.StaticText(self.frame_Queue,-1,'Region: - ',(20,25),(150,25))
LblMailbox = wx.StaticText(self.frame_Queue, '邮箱: - ',(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(se lf.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,'唯一键')
self.subList.InsertColumn(6,'Rem。SLA')
self.subList.InsertColumn(7, 'Aging')
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,'最终状态DtTm')
self.subList.InsertColumn(17,'最终状态日期')
#update listctrl
getConn = OutlookConnection()
messages = getConn.fetchUnallocated()
在消息中的msg:
self.subList.InsertStringItem(self.index,'')
self.subList.SetItemFo nt(self.index,listItems_Font)
self.subList.SetStringItem(self.index,1,msg.subject)
self.subList.SetStringItem(self.index,2,str(msg.receivedtime) )
如果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))
#在这里添加菜单
self.AddMenu()
#显示框
self.Centre()
self.Show()
#create AddMenu def
def AddMenu(self):
menuBar = wx.MenuBar()
#file菜单
File_btn = wx.Menu()
#sub菜单项文件菜单
#Logout
Logout_btn = File_btn.Append(wx.ID_EXIT,'& Logout','关闭应用程序')
#now将File_btn放入菜单栏
menuBar .Append(File_btn,'& File')
#设置应用程序主框架中的菜单栏
self.SetMenuBar(menuBar)
#now绑定将在点击退出按钮
self.Bind(wx.EVT_MENU,self.Quit,Logout_btn)
#def self.Quit进程
def Quit(self,x):
self.Close()
#class查询数据库
类dbQuery():
#method获取区域列表
def RegionsList(self):
myDb ='H:\\Python\\wxPython\\Programs\\Reference.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()
#运行查询并获取结果
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。
def __init __(self,parent,lstposition,lstsize):
super(myComboBox,self).__ init __(parent,-1,value =,pos = lstposition,size = lstsize)
#this方法将用于将列表中的项目添加到mycombobox的实例
def addItem(self,Lst = []):
在Lst中为itm:
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是指文件夹的索引 - 在这种情况下,收件箱
return inbox.Items
app = wx.App()
FIR_Frame = MainFrame(无,'FIR - TL接口')
app.MainLoop()
我正在面对的挑战在于我正在计算邮件项的老化的行: -
tm = datetime.now() - msg.receivedtime
self.subList.SetStringItem(self.index, 7,str(tm))
我收到错误: -
= datetime.now() - msg.receivedtime
TypeError:不支持的操作数类型 - :'datetime.datetime'和'time'
我已经尝试了更多的格式/方式...你可以在上面的代码中看到它们,导致类似的错误
有人可以帮助我计算 HH:MM:SS'格式。此外,如果老化超过24小时,小时数应该大于24。(如果老化超过24小时)。
提前谢谢。
问候,
Premanshu
有很多方法可以做到这一点,是一种方法。
检查 msg.receivedtime
的结构是否然后将其转换为几秒钟,以便能够操作它。我假设以下结构 msg.receivedtime
08-8-30 10:30:15
使用时间.time()
而不是 datetime.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)
>>>>打印h,m,s
1 17 27
编辑:关于日期戳更多的评论超过48小时
你必须做错什么!
这是上面的代码,使用time.time()的值作为2016年9月5日星期一。
>>>进口时间
>>>> 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天< br>
你需要什么:
有人可以帮助我计算HH:MM:SS '格式。此外,如果老化超过24小时,小时数应超过24个小时(如果老化超过24小时)。
I am trying to develop an application in wxPython in which I will fetch all e-mails into a listctrl and sort them based on their remaining SLA (3 days) for further action. For this I will calculate the ageing of the items in the mailbox by deducting the receivedTime from current time. Below is my complete code:-
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()
The challenge I am facing is in the line where I am trying to calculate the ageing of the mail item:-
tm = datetime.now() - msg.receivedtime
self.subList.SetStringItem(self.index, 7, str(tm))
I am getting error :- tm = datetime.now() - msg.receivedtime TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'time'
I have tried more formats/ways... you can see them in the above code resulting in similar errors
Can someone please help me with calculating the ageing in 'HH:MM:SS' format. Also the hours should populate in greater than 24 wherever required (i.e if ageing is more than 24 hours).
Thank you so much in advance.
Regards, Premanshu
There are many ways to do this, here is one way.
Check what the structure of msg.receivedtime
is and then convert it into seconds to be able to manipulate it. I am assuming the following structure of msg.receivedtime
"2016-08-30 10:30:15"
Use time.time()
rather than datetime.now()
to get 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
Note: I have reduced the time stamps to integer seconds in both cases
Edit:
to convert seconds to hh:mm:ss
>>> x = 4647
>>> m,s =divmod(x,60)
>>> h,m =divmod(m,60)
>>> print h,m,s
1 17 27
Edit: Concerning comment about date stamps more than 48 hours old
You must be doing something wrong!
Here is the code from above, using a value of time.time() as of Monday 5th September 2016.
>>> 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
Note that 144 hours is 6 days
Which is what you required:
Can someone please help me with calculating the ageing in 'HH:MM:SS' format. Also the hours should populate in greater than 24 wherever required (i.e if ageing is more than 24 hours).
这篇关于Python计算Outlook邮件项目的老化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!