How To Convert XLSX To Sheets In Google Drive API V3
I want to convert xlsx files automatically to Google spreadsheets when I upload them with my code to Google Drive. However, while the conversion works successfully for csv files, I get:
<HttpError 400 when requesting https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&alt=json returned "Bad Request">
when trying to upload xlsx.
Here is my code:
def upload_service(filepath, name="", description="", fileID="", parentID=""):
""" Uses a Resource (service) object to upload a file to drive. """
if service == "": authenticate_service()
if name == "":
name = str(os.path.basename(filepath).split(os.extsep)[0]) # Get from filepath
extension = str(os.path.basename(filepath).split(os.extsep)[1]).lower()
if extension == "csv": # CSV
mime_type = "text/csv"
elif extension in ["xls", "xlsx"]: # EXCEL
mime_type = "application/ms-excel"
else:
return
media_body = MediaFileUpload(filepath, mimetype=mime_type, resumable=True)
if parentID == "":
meta = dict(name=name, mimeType="application/vnd.google-apps.spreadsheet", description=description)
else:
meta = dict(name=name, mimeType="application/vnd.google-apps.spreadsheet", description=description, parents=[parentID])
if fileID == "": # CREATE
upload = service.files().create(
body=meta,
media_body=media_body).execute()
else: # REPLACE
upload = service.files().update(
body=meta,
media_body=media_body,
fileId=fileID).execute()
print ("\nFINISHED UPLOADING")
How can I do this in v3? It's very clear how to do it in v2, but not in the updated API.
Answer
In APIv3, you need to specify a very specific MIME Type for the conversion to occur.
At https://developers.google.com/drive/v3/web/manage-uploads#importing_to_google_docs_types_wzxhzdk8wzxhzdk9, you'll notice the statement "The supported conversions are available dynamically in the About resource's importFormats
array". Get the importFormats
list using either
GET https://www.googleapis.com/drive/v3/about?fields=importFormats&key={YOUR_API_KEY}
or by going to https://developers.google.com/drive/v3/reference/about/get#try-it and entering importFormats
You'll notice in the response:
"application/vnd.ms-excel": [
"application/vnd.google-apps.spreadsheet"
]
In your code, use:
elif extension in ["xls", "xlsx"]: # EXCEL
mime_type = "application/vnd.ms-excel"
(notice the additional vnd.
)and it should work well!
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module