无法使用Google Drive API复制Google幻灯片文件 [英] unable to copy a google slide file using google drive api

查看:83
本文介绍了无法使用Google Drive API复制Google幻灯片文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想复制Google驱动器中存在的现有模板ppt.然后,我想将占位符文本更改为其他一些文本.

I want to copy an existing template ppt present in my google drive. Then I want to change the placeholder text to some other text.

这是我正在尝试的.

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.

But it is not working. I don't get any error. everything works fine but I don't see the new 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()

然后它会替换文本,但会替换我不需要的模板文件中的文本.

then it does replace the text but in my template file which I don't want.

为什么会这样?

推荐答案

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

I believe your current situation and goal as follows.

  • 从您的脚本中,
    • 您正在将googleapis用于python.
    • 您已经能够使用服务帐户使用Drive API和Slides API.
    • 存在的现有模板ppt 是不是PowerPoint文件的Google幻灯片.
    • From your script,
      • You are using googleapis for python.
      • You have already been able to use Drive API and Slides API using the service account.
      • an existing template ppt present is a Google Slides which is not the PowerPoint file.
      • 通过您的脚本和,但是它不起作用.我没有任何错误.一切正常,但我看不到新的ppt.,我了解到您可能想查看服务帐户在Google云端硬盘中复制的Google幻灯片.

      • From your script and But it is not working. I don't get any error. everything works fine but I don't see the new ppt., I understood that you might want to see the Google Slides copied by the service account at your Google Drive.

      • 通过服务帐户复制Google幻灯片时,复制的Google幻灯片会放入服务帐户的云端硬盘中.云端硬盘服务帐户与您的Google云端硬盘不同.这样,您将无法在云端硬盘上看到复制的Google幻灯片.我认为这可能是您遇到问题的原因.

      例如,要查看服务帐户在您的Google云端硬盘中复制的Google幻灯片,可以使用以下变通办法.

      In order to see the Google Slides copied by the service account at your Google Drive, for example, the following workarounds can be used.

      1. 与您的Google帐户电子邮件共享复制的Google幻灯片.
        • 在这种情况下,您可以在共享文件夹中看到共享文件.

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

      In this workaround, it shares the copied Google Slides with your email of Google account. When this is reflected to your script, it becomes as follows.

      在这种情况下,将使用"Permissions:create"为复制的文件创建新的权限.

      In this case, new permission is created to the copied file using "Permissions: create".

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

      至:

      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云端硬盘中的共享文件夹中.在使用此脚本之前,请创建新文件夹并与服务帐户的电子邮件共享该文件夹.当这反映到您的脚本时,它如下所示.

      Workaround 2:

      In this workaround, the Google Slides is copied to the shared folder in your Google Drive. Before you use this script, please create new folder and share the folder with the email of service account. When this is reflected to your script, it becomes as follows.

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

      In this case, the metadata is added to the request body of DRIVE.files().copy().

      DATA = {'name': 'Google Slides API template DEMO'}
      

      至:

      DATA = {'name': 'Google Slides API template DEMO', 'parents': ['###']}
      

      • 请将共享文件夹的文件夹ID设置为 ### .
      • 这篇关于无法使用Google Drive API复制Google幻灯片文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆