我被困在这几个小时了。我有一个可以用python编辑的模板。其思想是复制、编辑、转换、下载文件,然后从驱动器中删除文件,只留下空模板。我已经阅读了文档并尝试了不同的方法,但我想不出来
编辑: 到目前为止我的代码
SCOPES = ['https://www.googleapis.com/auth/drive']
DOCUMENT_ID = '1ZgaYCra-7m_oWIBWK9RoMssYNTAsQLa1ELI1vyBB73c'
def main():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('docs', 'v1', credentials=creds)
text1 = 'test'
requests1 = [
{
'insertText': {
'location': {
'index': 110,
},
'text': text1
}
},
{
'insertText': {
'location': {
'index': 98,
},
'text': text1
}
},
{
'insertText': {
'location': {
'index': 83,
},
'text': text1
}
},
{
'insertText': {
'location': {
'index': 72,
},
'text': text1
}
},
{
'insertText': {
'location': {
'index': 49,
},
'text': text1
}
},
]
result = service.documents().batchUpdate(
documentId=DOCUMENT_ID, body={'requests': requests1}).execute()
如果name='main': main()
我相信你的现状和目标如下
修改点:
当您的脚本被修改时,它将变成如下所示
修改的脚本:
在这个修改过的脚本中,它假设从脚本中使用了
credentials=creds
中的creds
。在使用此脚本之前,请设置变量注:
import io
和from googleapiclient.http import MediaIoBaseDownload
李>requests1
forservice.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests1}).execute()
的请求主体工作正常,正如您所期望的那样。所以请小心这个李>参考文献:
相关问题 更多 >
编程相关推荐