0

First point : i have a question please , is there the possibility to download a pdf file from my drive as doc or any other format using Google Drive API? just as we can do manualy on drive : open a pdf file as google documents and download it .

EDIT : Second point : what i did : connect to apidrive via quickstart. I Turned on the Drive API(Step1) , and Installed the Google Client Library (step2). after that, i tryed to upload a file from my HD to my drive using the code :

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

the next step in my plan, was to download 'documt.pdf' that i've just uploaded to drive , in a different format (.doc for example). So i searched how to do So, i've found the following script , even if it doesn't do what i exactly want (as the docs says) , it can only download Google Documents as pdf(the inverse of what i wanted) :

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)

however even this code shows an error :

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

my client_secrets file looks like (don't know if it does have a relation to the problem): {"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"]}}

can anyone help me find a answer to my question (first point), and a solve the Error (second point)?

THANK YOU Very Much

Elroum
  • 327
  • 1
  • 3
  • 18

2 Answers2

0

I think that your script works fine. But from the error of NameError: name 'drive_service' is not defined, I thought that you may use Quickstart for Drive API. Your snippet on your question can be used as a part of the Quickstart. Your error is service = discovery.build('drive', 'v3', http=http) of Quickstart. In your case, the variable of drive_service is service at Quickstart. So the flow for using your snippet is as follows.

  1. Do Step 1 and Step 2 of Quickstart.
  2. For Step 3, modify service = discovery.build('drive', 'v3', http=http) to drive_service = discovery.build('drive', 'v3', http=http).
  3. Replace script below drive_service = discovery.build('drive', 'v3', http=http) in main() to your snippet. When you do this, please be careful the indent of your script.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • thank you for your answer. i was trying to do what you told me . but i have to inform you that i already did the step1 and 2 in order to upload some files. but now i want to do the downloading from my drive in other format(doc, txt ...) . that's why i found the script that i posted in my question. However,i did what you told me but the code in the 3rd point is only to get connected to drive and list the files. So do you have please an idea how can i download a file from drive using apidrive. – Elroum Oct 08 '17 at 16:34
  • @Elroum Thank you for your reply. In my environment, your script works fine. So can you show us your whole current script? If you can do it, please add it on your question. At that time, please remove your private information. – Tanaike Oct 08 '17 at 22:52
  • Thank you so much for your efforts. okey i'll show you what i've done exactly . i'll edit my question – Elroum Oct 09 '17 at 07:54
  • Can you please help me. i really need to solve the problem as soon as possible. Thank You very Much – Elroum Oct 09 '17 at 11:16
  • @Elroum I'm sorry for not being helpful. – Tanaike Oct 09 '17 at 22:34
  • it's okey. thank you very much for your caring and help. :-) – Elroum Oct 10 '17 at 08:53
0

Finnally, i've answered to my question. for the first point : it's not allowed to download a pdf as another format. However we can upload the file in the first place with the google docs format application/vnd.google-apps.document even if it's a pdf. and i will work really fine.

For the second point: because the 1st point issue is solved now, i don't need to fix the Error. cause what i wanted works perfectly good.

Elroum
  • 327
  • 1
  • 3
  • 18