Reputation: 26387
I want to download a Google doc as an .odt
-file. There's a previous question on how to download normal files from Google Drive.
I want to download the Doc with Python, because I then want to continue to work with it within my slave.
Upvotes: 0
Views: 644
Reputation: 1057
Download Google Workspace Documents
There are two things that I would like to clarify:
One important thing to note is that in order to download Google Workspace documents you would need to export them to a file type that allows download.
The Drive API does support OpenDocument. A sample of my code to export to .odt
from an ID of a Google Doc:
```
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload
SCOPES = ['https://www.googleapis.com/auth/drive']
def main():
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
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)
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
service = build('drive', 'v3', credentials=creds)
file_id = "fileID_in_Google_Drive"
data = service.files().export(fileId=file_id, mimeType="application/vnd.oasis.opendocument.text").execute()
if data:
filename = 'your-file-name.odt'
with open(filename, 'wb') as pdf_file:
pdf_file.write(data)
except HttpError as error:
print(F'An error occurred: {error}')
file = None
if __name__ == '__main__':
main()
You can change the:
file_id = "fileID_in_Google_Drive"
With the ID of a Google Doc to run the test and verify it. This allowed me to modify and test the .odf
. Editing the MIME type to application/vnd.oasis.opendocument.formula
. This would throw the error:
An error occurred: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/files/fileID_in_Google_Drive/export?mimeType=application%2Fvnd.oasis.opendocument.formula returned "The requested conversion is not supported.". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'The requested conversion is not supported.', 'locationType': 'parameter', 'location': 'convertTo'}]">
Confirming that the Drive API V3 does not support the export to .odf
.
As an alternative you can document and request a feature for the API, you should be able to open an Issue tracker:
Edit:
PIP imports:
References
Upvotes: 1