Python Outlook日历导出类型问题

Python Outlook日历导出类型问题,python,pandas,pywin32,Python,Pandas,Pywin32,我有以下代码用于提取outlook日历,并向我显示已安排会议的所有参与者的列表。。我遇到了以下与数据类型相关的错误。我相信问题实际上是让事件拉入,因为当我在错误之前打印约会列表时,它显示为空白。想法 代码: 将日期时间导入为dt 作为pd进口熊猫 导入win32com.client def get_日历(开始、结束): outlook=win32com.client.Dispatch('outlook.Application').GetNamespace('MAPI')) 日历=outlook.

我有以下代码用于提取outlook日历,并向我显示已安排会议的所有参与者的列表。。我遇到了以下与数据类型相关的错误。我相信问题实际上是让事件拉入,因为当我在错误之前打印约会列表时,它显示为空白。想法

代码:

将日期时间导入为dt
作为pd进口熊猫
导入win32com.client
def get_日历(开始、结束):
outlook=win32com.client.Dispatch('outlook.Application').GetNamespace('MAPI'))
日历=outlook.getDefaultFolder(9).Items
calendar.includeCurrences=True
calendar.Sort(“[Start]”)

restriction=“[Start]>=”+begin.strftime(“%m/%d/%Y”)+”和[END]两个
datetime
对象的减法将产生一个
timedelta
对象。为了从timedelta对象检索小时,可以使用:

将numpy导入为np
小时=timedelta_对象/np.timedelta64(1,“h”)
注意:也可以是(仅限熊猫风格)

因此,在您的情况下,您可以将其用作:

约会['Hours']=(约会['end']-约会['start'])/pd.Timedelta(小时=1)

newerror posted…看起来像是一个类似的问题。我明白了(我想)。你的
appoints[“start”]
appoints[“end”]
是什么样子的?它们的类型是什么?我假设是datetime对象,但可能不是
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", 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']) / 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]')
hours = timedelta_object / pd.Timedelta(hours=1)