简体   繁体   English

我想将谷歌驱动器的文档导出为 pdf | 谷歌驱动器 Api Python

[英]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.嗨,很高兴见到你:),我正在编写一个应用程序,其中一部分使用一种方法从谷歌文档下载文档 Docx 以将其导出为 pdf,但我不知道在哪里可以看到 python 中的下载文件。 I'm coding with the google docs API documentation example from this URL: https://developers.google.com/drive/api/v3/manage-downloads#python我正在使用来自此 URL 的谷歌文档 API 文档示例进行编码: https://developers.google.com/drive/api/v3/

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.终端显示下载已完成,但我在任何地方都看不到文件 PD:我是 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!如果有人知道更好的方法或指南来完成它,我将非常感激:D!
This is a project that I'm working on my internship of software Engineering这是我正在从事软件工程实习的一个项目

Best Regards此致

with open(output_filename, "wb") as output:
    while True:
        data = downloader.read()
        if data == "":
            break
        output.write(data)

https://stackoverflow.com/a/6787266/17613030 https://stackoverflow.com/a/6787266/17613030

bu sekilde deneyin downloader ile veriyi aliyorsunuz bu sekilde deneyin 下载器 ile veriyi aliyorsunuz

I already found the solution:D我已经找到了解决方案: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.编辑:我忘记了一件非常重要的事情,您还需要将shutil导入代码。

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此致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM