如何解决Outlook 日历导出类型问题
我有以下代码用于提取我的 Outlook 日历并向我显示我安排的会议中所有参与者的列表。我遇到了以下与数据类型相关的错误。我相信问题实际上是让事件被拉取,因为当我在错误之前打印约会列表时,它显示为空白。想法?
代码:
import datetime as dt
import pandas as pd
import win32com.client
def get_calendar(begin,end):
outlook = win32com.client.Dispatch('Outlook.Application').GetNamespace('MAPI')
calendar = outlook.getDefaultFolder(9).Items
calendar.IncludeRecurrences = True
calendar.Sort('[Start]')
restriction = "[Start] >= '" + begin.strftime('%m/%d/%Y') + "' AND [END] <= '" + end.strftime('%m/%d/%Y') + "'"
calendar = calendar.Restrict(restriction)
return calendar
def get_appointments(calendar,subject_kw = None,exclude_subject_kw = None,body_kw = None):
if subject_kw == None:
appointments = [app for app in calendar]
else:
appointments = [app for app in calendar if subject_kw in app.subject]
if exclude_subject_kw != None:
appointments = [app for app in appointments if exclude_subject_kw not in app.subject]
cal_subject = [app.subject for app in appointments]
cal_start = [app.start for app in appointments]
cal_end = [app.end for app in appointments]
cal_body = [app.body for app in appointments]
df = pd.DataFrame({'subject': cal_subject,'start': cal_start,'end': cal_end,'body': cal_body})
return df
def make_cpd(appointments):
appointments['Date'] = appointments['start']
appointments['Hours'] = (appointments['end'] - appointments['start']).dt.seconds/3600
appointments.rename(columns={'subject':'Meeting Description'},inplace = True)
appointments.drop(['start','end'],axis = 1,inplace = True)
summary = appointments.groupby('Meeting Description')['Hours'].sum()
return summary
final = r"C:\Users\rcarmody\Desktop\Python\Accelerators\Outlook Output.xlsx"
begin = dt.datetime(2021,1,1)
end = dt.datetime(2021,5,12)
print(begin)
print(end)
cal = get_calendar(begin,end)
appointments = get_appointments(cal,subject_kw = 'weekly',exclude_subject_kw = 'Webcast')
result = make_cpd(appointments)
result.to_excel(final)
错误:
Traceback (most recent call last):
File "C:\Users\Desktop\Python\Accelerators\outlook_meetings.py",line 50,in <module>
result = make_cpd(appointments)
File "C:\Users\Desktop\Python\Accelerators\outlook_meetings.py",line 34,in make_cpd
appointments['Hours'] = (appointments['end'] - appointments['start']).dt.seconds/3600
File "C:\Users\AppData\Roaming\Python\Python39\site-packages\pandas\core\generic.py",line 5461,in __getattr__
return object.__getattribute__(self,name)
File "C:\Users\rcarmody\AppData\Roaming\Python\Python39\site-packages\pandas\core\accessor.py",line 180,in __get__
accessor_obj = self._accessor(obj)
File "C:\Users\AppData\Roaming\Python\Python39\site-packages\pandas\core\indexes\accessors.py",line 494,in __new__
raise AttributeError("Can only use .dt accessor with datetimelike values")
AttributeError: Can only use .dt accessor with datetimelike values
[Finished in 1.2s]
新错误:
Traceback (most recent call last):
File "C:\Users\Desktop\Python\Accelerators\outlook_meetings.py",in make_cpd
appointments['Hours'] = (appointments['end'] - appointments['start']) / pd.Timedelta(hours=1)
File "C:\Users\\AppData\Roaming\Python\Python39\site-packages\pandas\core\ops\common.py",line 65,in new_method
return method(self,other)
File "C:\Users\AppData\Roaming\Python\Python39\site-packages\pandas\core\arraylike.py",line 113,in __truediv__
return self._arith_method(other,operator.truediv)
File "C:\Users\\AppData\Roaming\Python\Python39\site-packages\pandas\core\series.py",line 4998,in _arith_method
result = ops.arithmetic_op(lvalues,rvalues,op)
File "C:\Users\\AppData\Roaming\Python\Python39\site-packages\pandas\core\ops\array_ops.py",line 185,in arithmetic_op
res_values = op(lvalues,rvalues)
File "pandas\_libs\tslibs\timedeltas.pyx",line 1342,in pandas._libs.tslibs.timedeltas.Timedelta.__rtruediv__
numpy.core._exceptions.UFuncTypeError: ufunc 'true_divide' cannot use operands with types dtype('float64') and dtype('<m8[ns]')
解决方法
两个 datetime
对象相减产生一个 timedelta
对象。为了从 timedelta 对象中检索小时,您可以使用:
import numpy as np
hours = timedelta_object / np.timedelta64(1,"h")
注意:也可以是(更多熊猫风格)
hours = timedelta_object / pd.Timedelta(hours=1)
因此,在您的情况下,您可以将其用作:
appointments['Hours'] = (appointments['end'] - appointments['start']) / pd.Timedelta(hours=1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。