Outlook 日历导出类型问题

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-