如何通过Google Calendar API从列表添加参会者邮箱
解决Google Calendar事件添加参会者列表的问题
你可以用列表推导式把邮箱列表转换成Google Calendar API要求的格式,直接替换原代码里硬编码的参会者部分即可,具体操作如下:
步骤1:转换邮箱列表格式
把你的attends纯邮箱字符串列表,转换成API需要的字典结构列表:
# 将每个邮箱字符串包装成{'email': 邮箱}的字典 attendees_list = [{'email': email} for email in attends]
步骤2:修改event对象的attendees字段
把原代码里硬编码的attendees替换成上面生成的attendees_list,修改后的完整函数代码如下:
def submit(): summary = title.get() location = local.get() s_date = start_date.get() s_time = start_time.get() final_s_date = s_date + "T" + s_time + ":00" e_date = end_date.get() e_time = end_time.get() final_e_date = e_date + "T" + e_time + ":00" service2 = get_calendar_api() # 转换参会者列表格式 attendees_list = [{'email': email} for email in attends] event = { 'summary': summary, 'location': location, 'description': 'Empty', 'start': { 'dateTime': final_s_date, 'timeZone': 'Asia/Kuala_Lumpur', }, 'end': { 'dateTime': final_e_date, 'timeZone': 'Asia/Kuala_Lumpur', }, 'attendees': attendees_list, # 替换为转换后的参会者列表 } service2.events().insert(calendarId='primary', body=event).execute()
补充提示
- Google Calendar API要求
attendees字段必须是字典列表,每个字典需包含email键对应参会者邮箱 - 如果
attends列表可能存在空字符串或无效邮箱,可添加过滤逻辑避免报错:attendees_list = [{'email': email} for email in attends if email.strip()]
内容的提问来源于stack exchange,提问作者Lohan Vidanage
相关产品推荐
相关产品推荐

