GoogleSlidesAPI开发
Google Slides 是 Google 提供的在线演示文稿程序,可以通过 API 动态生成和修改文档。
- presentation:PPT 演示文稿文档。
- slide:代表每一页幻灯片。
- notesMaster:备注页面信息。
- layouts:幻灯片布局模板。
- masters:母版信息。
3. API 使用方式
Section titled “3. API 使用方式”3.1. 原生 API
Section titled “3.1. 原生 API”- 创建文档
response = self.service.presentations().create(body=body).execute()- 更改文档
# requests 代表请求操作列表response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 获取文档元数据
response = self.service.presentations().get(presentationId=file_id).execute()- 获取指定 slide
response = self.service.presentations().pages().get( presentationId=file_id, pageObjectId=page_id).execute()3.2. 封装 API
Section titled “3.2. 封装 API”- 复制对象
copy_requests.append({ "duplicateObject": { "objectId": self.slide_id_map[slide_template], "objectIds": item["slide_map"] }})- 更新 slide 排序位置
position_requests.append({ "updateSlidesPosition": { "slideObjectIds": [slide_template], "insertionIndex": int(index) }})- 删除对象
remove_requests.append({ "deleteObject": { "objectId": item, }})- 替换文本占位符
requests = []for item in req: requests.append({ 'replaceAllText': { 'containsText': { 'text': item["placeholder"], 'matchCase': True }, "pageObjectIds": item["objectIds"], 'replaceText': item["replaceText"] } })response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 替换图片
requests = []for item in req: requests.append({ 'replaceImage': { 'imageReplaceMethod': item["replaceMethod"] if "replaceMethod" in item else "CENTER_INSIDE", # 还支持 CENTER_CROP "imageObjectId": item["imageId"], 'url': item["url"] } })response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 将文本框替换为图片
requests = []for item in req: requests.append({ "replaceAllShapesWithImage": { 'containsText': { 'text': item["placeholder"], 'matchCase': True }, "imageUrl": item["url"], "pageObjectIds": item["objectIds"], } })response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 文本框添加超链接
requests = []for item in req: requests.append({ "updateTextStyle": { "objectId": item["objectId"], "textRange": { "startIndex": item["start_index"], "endIndex": item["end_index"], "type": "FIXED_RANGE" }, "style": { "link": { "pageObjectId": item['pageObjectId'] } }, "fields": "link", } })response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 表格单元格文本添加超链接
requests = []for item in req: requests.append({ "updateTextStyle": { "objectId": item["objectId"], "cellLocation": { "rowIndex": item["row_index"], "columnIndex": item["column_index"], }, "style": { "link": { "pageObjectId": item['pageObjectId'] } }, "fields": "link", } })response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 修改文本框内文本颜色
requests = []for item in req: stage = { "updateTextStyle": { "objectId": item["objectId"], "style": { "foregroundColor": { "opaqueColor": { "themeColor": item["color"] } } }, "fields": "foregroundColor", } } if "start_index" in item: stage["updateTextStyle"]["textRange"] = { "startIndex": item["start_index"], "endIndex": item["end_index"], "type": "FIXED_RANGE" } requests.append(stage)response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 修改表格单元格内文本颜色
requests = []for item in req: requests.append({ "updateTextStyle": { "objectId": item["objectId"], "cellLocation": { "rowIndex": item["row_index"], "columnIndex": item["column_index"], }, "style": { "foregroundColor": { "opaqueColor": { "themeColor": item["color"] } } }, "fields": "foregroundColor", } })response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 修改图形边框颜色
requests = []for item in req: requests.append({ "updateShapeProperties": { "objectId": item["objectId"], "shapeProperties": { "outline": { "outlineFill": { "solidFill": { "color": { "themeColor": item["color"] } } } } }, "fields": "outline.outlineFill.solidFill.color", } })response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 修改背景填充颜色
requests = []for item in req: requests.append({ "updateShapeProperties": { "objectId": item["objectId"], "shapeProperties": { "shapeBackgroundFill": { "solidFill": { "color": { "themeColor": item["color"] } } } }, "fields": "shapeBackgroundFill.solidFill.color", } })response = self.service.presentations().batchUpdate( body={"requests": requests}, presentationId=presentation_id).execute()- 新创建或复制的文档默认存储在 Google Drive 的根目录。如果需要整理结构,请使用 Google Drive API 进行目录更新。
- 在发送
update请求时,可以指定revision_id进行乐观锁冲突控制。若不指定,系统默认基于最新版本合并提交。
5. 拓展信息
Section titled “5. 拓展信息”5.1. API 限制
Section titled “5.1. API 限制”- 仅存在调用频率限制,具体细节请 参考频率限制官方文档。
5.2. 图表操作限制
Section titled “5.2. 图表操作限制”- 插入的 Slide 图表底层均依赖 Google Sheet 数据源,并在 Slide 中以静态图片形式渲染呈现。如果 Google Sheet 源数据更新,必须主动调用刷新接口重新渲染图片。
- 支持通过 API 替换特定 Shape 为指定 Google Sheet 的 Chart。
- 缺点:将 Slide 导出并下载为 PPTX 格式时,图表会直接固化为静态图片格式,在本地 PowerPoint 中无法双击编辑数据。
5.3. 图片 URL 响应模式
Section titled “5.3. 图片 URL 响应模式”- 预览模式:
Content-Disposition: inline,浏览器或 API 直接解析渲染。 - 下载模式:
Content-Disposition: attachment,浏览器打开直接下载。