我想复制谷歌硬盘中现有的模板ppt。然后我想将占位符文本更改为其他文本
这就是我正在尝试的
from google.oauth2 import service_account
import googleapiclient.discovery
SCOPES = (
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/presentations',
)
SERVICE_ACCOUNT_FILE = 'cred.json'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
SLIDES = discovery.build('slides', 'v1', credentials=credentials)
DRIVE = discovery.build('drive', 'v3', credentials=credentials)
TMPLFILE = 'title slide template'
rsp = DRIVE.files().list(q="name='%s'" % TMPLFILE).execute().get('files')[0]
print(rsp)
DATA = {'name': 'Google Slides API template DEMO'}
print('** Copying template %r as %r' % (rsp['name'], DATA['name']))
DECK_ID = DRIVE.files().copy(body=DATA, fileId=rsp['id']).execute().get('id')
print(DECK_ID)
print('** Replacing placeholder text')
reqs = [
{'replaceAllText': {
'containsText': {'text': '{{text}}'},
'replaceText': final_til[0]
}},
]
SLIDES.presentations().batchUpdate(body={'requests': reqs},
presentationId=DECK_ID).execute()
print('DONE')
但它不起作用。我没有得到任何错误。一切正常,但我没有看到新的ppt
输出:
{'kind': 'drive#file', 'id': '15mVjkrT7PkckKetK_q9aYRVxaDcwDdHpAh7xjrAWB6Q', 'name': 'title slide template', 'mimeType': 'application/vnd.google-apps.presentation'} <--- rsp
** Copying template 'title slide template' as 'Google Slides API template DEMO'
11O97tySSNaboW6YRVD62Q7HLs8aVuS2pWyLYXImdSec <-- DECK_ID
** Replacing placeholder text
DONE
如果我改变
SLIDES.presentations().batchUpdate(body={'requests': reqs},
presentationId=DECK_ID).execute()
到
SLIDES.presentations().batchUpdate(body={'requests': reqs},
presentationId=rsp.get('id')).execute()
然后,它确实替换了文本,但在我的模板文件中,我不想要它
为什么会这样
@Tanaike的回答很好,但还有一个选择:
帐户模拟
下面是一个很好的概述:Using OAuth 2.0 for Server to Server Applications,特别是this section遍历了代码
请记住在GCP控制台和管理控制台中设置域范围的委派
在GCP云控制台中初始化的项目也已从管理控制台中被授予作用域>;安全性>;API控制>;域范围授权>;新增
脚本要做的第一件事是使用
from_service_account_file
构建凭据:然后它构建委托的凭证,即要模拟的用户:
从那里,它可以正常地构建服务。您可以将其保存到用户的驱动器,就像用户自己在做一样
参考文献
我相信你的现状和目标如下
an existing template ppt present
是一个不是PowerPoint文件的Google幻灯片李>修改点:
从您的脚本和
But it is not working. I don't get any error. everything works fine but I don't see the new ppt.
中,我了解到您可能希望在您的Google驱动器上看到由服务帐户复制的Google幻灯片例如,为了查看Google Drive上服务帐户复制的Google幻灯片,可以使用以下变通方法
解决方法1:
在此解决方案中,它将复制的谷歌幻灯片与您的谷歌帐户电子邮件共享。当这反映到脚本中时,它将变成如下所示
修改的脚本:
在这种情况下,将使用“权限:创建”为复制的文件创建新权限
发件人: 致:解决方法2:
在此解决方案中,谷歌幻灯片将复制到谷歌驱动器中的共享文件夹中。在使用此脚本之前,请创建新文件夹并与服务帐户的电子邮件共享该文件夹。当这反映到脚本中时,它将变成如下所示
修改的脚本:
在这种情况下,元数据被添加到
发件人: 致:DRIVE.files().copy()
的请求主体中###
李>参考资料:
相关问题 更多 >
编程相关推荐