首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Outlook日历导出类型问题

Outlook日历导出类型问题
EN

Stack Overflow用户
提问于 2021-05-13 02:47:06
回答 1查看 37关注 0票数 0

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

代码:

代码语言:javascript
运行
复制
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)

错误:

代码语言:javascript
运行
复制
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]

新错误:

代码语言:javascript
运行
复制
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]')
EN

回答 1

Stack Overflow用户

发布于 2021-05-13 03:07:44

两个datetime对象的减法得到一个timedelta对象。为了从timedelta对象中检索小时数,您可以使用:

代码语言:javascript
运行
复制
import numpy as np

hours = timedelta_object / np.timedelta64(1, "h")

注意:它也可能是(更多仅限熊猫的风格)

代码语言:javascript
运行
复制
hours = timedelta_object / pd.Timedelta(hours=1)

因此,在您的示例中,您可以将其用作:

代码语言:javascript
运行
复制
appointments['Hours'] = (appointments['end'] - appointments['start']) / pd.Timedelta(hours=1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67509407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档