无法使用 Google Drive api 复制 Google 幻灯片文件

2024-04-03

我想复制我的谷歌驱动器中存在的现有模板 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。

Output:

{'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()

to

SLIDES.presentations().batchUpdate(body={'requests': reqs},
            presentationId=rsp.get('id')).execute()

然后它确实替换了我不想要的模板文件中的文本。

为什么会发生这种情况?


我相信您目前的情况和目标如下。

  • From your script,
    • 您正在使用 googleapis for python。
    • 您已经能够通过服务帐户使用 Drive API 和 Slides API。
    • an existing template ppt present是 Google 幻灯片,不是 PowerPoint 文件。

修改要点:

  • 从你的脚本和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 幻灯片时,复制的 Google 幻灯片将被放入服务帐户的云端硬盘中。服务帐户的云端硬盘与您的 Google 云端硬盘不同。这样,您就无法在云端硬盘上看到复制的 Google 幻灯片。我认为这可能是您问题的原因。
  • 例如,为了查看 Google 云端硬盘上的服务帐户复制的 Google 幻灯片,可以使用以下解决方法。

    1. Share the copied Google Slides with your email of Google account.
      • 此时,您可以在共享文件夹中看到共享文件。
    2. 首先,它会在您的 Google 云端硬盘中创建新文件夹,并与服务帐户的电子邮件共享该文件夹。当复制 Google 幻灯片时,它将共享文件夹设置为目标文件夹。

解决方法1:

在此解决方法中,它会与您的 Google 帐户电子邮件共享复制的 Google 幻灯片。当这反映到您的脚本中时,它会变成如下所示。

修改后的脚本:

在这种情况下,使用“权限:创建”为复制的文件创建新权限。

From:
print(DECK_ID)

print('** Replacing placeholder text')
To:
print(DECK_ID)

permission = {
    'type': 'user',
    'role': 'writer',
    'emailAddress': '###@gmail.com'  # <--- Please set the email of your Google account.
}
DRIVE.permissions().create(fileId=DECK_ID, body=permission).execute()

print('** Replacing placeholder text')

解决方法2:

在此解决方法中,Google 幻灯片将复制到 Google 云端硬盘中的共享文件夹。在使用此脚本之前,请创建新文件夹并与服务帐户的电子邮件共享该文件夹。当这反映到您的脚本中时,它会变成如下所示。

修改后的脚本:

在这种情况下,元数据被添加到请求正文中DRIVE.files().copy().

From:
DATA = {'name': 'Google Slides API template DEMO'}
To:
DATA = {'name': 'Google Slides API template DEMO', 'parents': ['###']}
  • 请将共享文件夹的文件夹ID设置为###.

参考:

  • 权限:创建 https://developers.google.com/drive/api/v3/reference/permissions/create
  • 文件:复制 https://developers.google.com/drive/api/v3/reference/files/copy
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用 Google Drive api 复制 Google 幻灯片文件 的相关文章

随机推荐