Python : 使用 apidrive 从驱动器下载另一种格式的文件
Python : download a file from drive in another format using apidrive
第一点:我有一个问题,是否可以使用 Google 驱动器 API 从我的驱动器下载 pdf 文件作为 doc 或任何其他格式?
就像我们可以在驱动器上手动操作一样:打开一个 pdf 文件作为 google 文档并下载它。
编辑:
第二点:我做了什么:
通过快速启动连接到 apidrive。我打开了驱动器 API(Step1) ,并安装了 Google 客户端库(step2)。
之后,我尝试使用以下代码将文件从我的硬盘上传到我的驱动器:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
#1st authentification
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles
authentication.
drive = GoogleDrive(gauth)
file1 = drive.CreateFile()
file1.SetContentFile('documt.pdf')
file1.Upload()
我计划的下一步是以不同的格式(例如 .doc)下载我刚刚上传到驱动器的 'documt.pdf'。所以我搜索了如何做所以,我找到了以下脚本,即使它没有做我真正想要的(如文档所说),它也只能下载 Google 文档作为 pdf(相反的我想要的):
file_id = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'
request = drive_service.files().export_media(fileId=file_id,
mimeType='application/pdf')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download %d%%." % int(status.progress() * 100)
然而即使这段代码也显示错误:
Traceback (most recent call last):
File "C:\Users\OU\AppData\Local\Programs\Python\Python36-
32\download_from_drive.py", line 10, in <module>
request = drive_service.files().export_media(fileId=file_id,
NameError: name 'drive_service' is not defined
我的client_secrets文件看起来像(不知道它是否与问题有关):
{"web":{"client_id":"xxxxx.apps.googleusercontent.com","project_id":"xxxx","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/x/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/c","client_secret":"erm-xxxx","redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"]}}
谁能帮我找到问题的答案(第一点),并解决错误(第二点)?
非常感谢
我认为您的脚本运行良好。但是从NameError: name 'drive_service' is not defined
的错误来看,我认为你可以使用Quickstart for Drive API。您的问题片段可以用作快速入门的一部分。您的错误是快速入门的 service = discovery.build('drive', 'v3', http=http)
。在您的例子中, drive_service
的变量在 Quickstart 中是 service
。因此使用您的代码段的流程如下。
- 执行 Quickstart 的第 1 步和第 2 步。
- 对于第 3 步,将
service = discovery.build('drive', 'v3', http=http)
修改为 drive_service = discovery.build('drive', 'v3', http=http)
。
- 将
main()
中 drive_service = discovery.build('drive', 'v3', http=http)
下面的脚本替换为您的代码段。执行此操作时,请注意脚本的缩进。
最后,我已经回答了我的问题。
对于第一点:不允许将 pdf 下载为另一种格式。但是,我们可以首先使用 google 文档格式 application/vnd.google-apps.document
上传文件,即使它是 pdf。我会好好工作的。
第二点:因为第一点的问题现在已经解决了,所以我不需要修复错误。因为我想要的效果非常好。
第一点:我有一个问题,是否可以使用 Google 驱动器 API 从我的驱动器下载 pdf 文件作为 doc 或任何其他格式? 就像我们可以在驱动器上手动操作一样:打开一个 pdf 文件作为 google 文档并下载它。
编辑: 第二点:我做了什么: 通过快速启动连接到 apidrive。我打开了驱动器 API(Step1) ,并安装了 Google 客户端库(step2)。 之后,我尝试使用以下代码将文件从我的硬盘上传到我的驱动器:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
#1st authentification
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles
authentication.
drive = GoogleDrive(gauth)
file1 = drive.CreateFile()
file1.SetContentFile('documt.pdf')
file1.Upload()
我计划的下一步是以不同的格式(例如 .doc)下载我刚刚上传到驱动器的 'documt.pdf'。所以我搜索了如何做所以,我找到了以下脚本,即使它没有做我真正想要的(如文档所说),它也只能下载 Google 文档作为 pdf(相反的我想要的):
file_id = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'
request = drive_service.files().export_media(fileId=file_id,
mimeType='application/pdf')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download %d%%." % int(status.progress() * 100)
然而即使这段代码也显示错误:
Traceback (most recent call last):
File "C:\Users\OU\AppData\Local\Programs\Python\Python36-
32\download_from_drive.py", line 10, in <module>
request = drive_service.files().export_media(fileId=file_id,
NameError: name 'drive_service' is not defined
我的client_secrets文件看起来像(不知道它是否与问题有关): {"web":{"client_id":"xxxxx.apps.googleusercontent.com","project_id":"xxxx","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/x/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/c","client_secret":"erm-xxxx","redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"]}}
谁能帮我找到问题的答案(第一点),并解决错误(第二点)?
非常感谢
我认为您的脚本运行良好。但是从NameError: name 'drive_service' is not defined
的错误来看,我认为你可以使用Quickstart for Drive API。您的问题片段可以用作快速入门的一部分。您的错误是快速入门的 service = discovery.build('drive', 'v3', http=http)
。在您的例子中, drive_service
的变量在 Quickstart 中是 service
。因此使用您的代码段的流程如下。
- 执行 Quickstart 的第 1 步和第 2 步。
- 对于第 3 步,将
service = discovery.build('drive', 'v3', http=http)
修改为drive_service = discovery.build('drive', 'v3', http=http)
。 - 将
main()
中drive_service = discovery.build('drive', 'v3', http=http)
下面的脚本替换为您的代码段。执行此操作时,请注意脚本的缩进。
最后,我已经回答了我的问题。
对于第一点:不允许将 pdf 下载为另一种格式。但是,我们可以首先使用 google 文档格式 application/vnd.google-apps.document
上传文件,即使它是 pdf。我会好好工作的。
第二点:因为第一点的问题现在已经解决了,所以我不需要修复错误。因为我想要的效果非常好。