Python + google drive: download xlsx, convert to google sheet, get available link

The flow of my desired program:

  • Download the xlsx table to disk (it was created using pandas to_excel )
  • Convert it to Google Sheets.
  • Indicate that it is editable by anyone with a link.
  • Get a link and share it with whoever will enter information
  • Download the completed sheet

I am currently using PyDrive, which solves steps 1 and 5, but there are some unresolved issues.

How can I convert to google listing format? I tried to specify mimeType as 'application/vnd.google-apps.spreadsheet' when I created a file to download from PyDrive, but that gave me an error.

How to set up a file for editing by anyone who has a link? Once this is installed, I can easily get a sharing link with PyDrive.

UPDATE: Switching from xlsx to Google Sheets is easy with the convert=True flag. See below. I'm still looking for a way to set sharing options for my new file so that "someone with a link can edit."

 from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive gauth = GoogleAuth() gauth.LocalWebserverAuth() drive = GoogleDrive(gauth) test_file = drive.CreateFile({'title': 'testfile.xlsx'}) test_file.SetContentFile('testfile.xlsx') test_file.Upload({'convert': True}) 
+8
python google-spreadsheet google-drive-sdk pydrive
source share
2 answers

There is an optional query parameter "convert" for both the INSERT method and the COPY method;

 convert=true, 

Convert this file to the appropriate Google Docs format. (Default: false)

Here is a python example:

Google Documentation - Copy

You need to use the Python client library to run the code.

 from apiclient import errors from apiclient.http import MediaFileUpload # ... def insert_file(service, title, description, parent_id, mime_type, filename): """Insert new file. Args: service: Drive API service instance. title: Title of the file to insert, including the extension. description: Description of the file to insert. parent_id: Parent folder ID. mime_type: MIME type of the file to insert. filename: Filename of the file to insert. Returns: Inserted file metadata if successful, None otherwise. """ media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True) body = { 'title': title, 'description': description, 'mimeType': mime_type } # Set the parent folder. if parent_id: body['parents'] = [{'id': parent_id}] try: file = service.files().insert( body=body, convert=true, media_body=media_body).execute() # Uncomment the following line to print the File ID # print 'File ID: %s' % file['id'] return file except errors.HttpError, error: print 'An error occured: %s' % error return None 

I have not tried this, so you need to test it.

+3
source share

To configure a file for editing for anyone with a link, you need to insert a new permission with the following information:

 from apiclient import errors # ... def share_with_anyone(service, file_id): """Shares the file with anyone with the link Args: service: Drive API service instance. file_id: ID of the file to insert permission for. Returns: The inserted permission if successful, None otherwise. """ new_permission = { 'type': "anyone", 'role': "writer", 'withLink': True } try: return service.permissions().insert( fileId=file_id, body=new_permission).execute() except errors.HttpError, error: print 'An error occurred: %s' % error return None 

then to get the link you are going to: file ["alternateLink"]

+2
source share

All Articles