Search code examples
pythonapigoogle-apigoogle-drive-apigoogle-docs-api

I want to export a document of google drive as pdf | Google Drive Api Python


Hi Nice to meet you : ), I'm coding an app that one part uses a method to download a document Docx from google docs to export it as pdf but I don't know where I can see the downloaded files in python. I'm coding with the google docs API documentation example from this URL: https://developers.google.com/drive/api/v3/manage-downloads#python

This is my code :

#Block 3 : Convertir el Documento en PDF
            print("Se inicia la conversion a PDF")
            time.sleep(4)
            service = build('drive', 'v3', credentials=creds)
            
            request = service.files().export_media(fileId=documentId,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))
            
            print("Se ha completado la descarga")

and the terminal says the download was complete but I can't see the Files anywhere PD: I'm a beginner with python.

This is what the terminal says :

Se inicia la conversion a PDF
Download 100%.
Se ha completado la descarga

If someone knows a better method or a guide to make it done I will be very grateful :D!
This is a project that I'm working on my internship of software Engineering

Best Regards


Solution

  • I already found the solution :D

    Before Changes :

    #Block 3 : Convertir el Documento en PDF
                print("Se inicia la conversion a PDF")
                time.sleep(4)
                service = build('drive', 'v3', credentials=creds)
                
                request = service.files().export_media(fileId=documentId,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))
                
                print("Se ha completado la descarga")
    

    After Changes (Solution) :

    #Block 3 : Convertir el Documento en PDF
                print("Se inicia la conversion a PDF")
                time.sleep(4)
                service = build('drive', 'v3', credentials=creds)
                
                request = service.files().export_media(fileId=documentId,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))
                
    
                fh.seek(0)
                with open('newpdfboleta.pdf', 'wb') as f:
                    shutil.copyfileobj(fh, f, length=131072)
               
                print("Se ha completado la descarga")
                print(fh)
            
    

    Edit: I forgot a very important thing , you also need to import shutil to the code.

    import shutil
    

    Thank you very much to the people who tried to help me! If you have questions about this, you can ask me anytime! you're Welcome.

    Best Regards