How to Convert XLSX to Sheets in Google Drive API v3

人盡茶涼 提交于 2019-12-07 14:24:36

问题


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.


回答1:


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!




回答2:


Based on the Official Google Documentation, You received 400: Bad Request meaning that required field or parameter has not been provided, the value supplied is invalid or the combination of provided fields is invalid. This error can be thrown when trying to add a parent that would create a cycle in the directory graph.

When encountering this error, the suggested action is to use exponential backoff. it is a standard error handling strategy for network applications in which the client periodically retries a failed request over an increasing amount of time.

You can use the Official Google Docs for your reference, There is a parameter convert convert=true, to convert file to the corresponding Google Docs format (Default:false).

You also need to use the Python client library, you can use that library to support uploading files.

Found this Stack Overflow ticket, check the solution offered by community: python + google drive: upload xlsx, convert to google sheet, get sharable link



来源:https://stackoverflow.com/questions/36948862/how-to-convert-xlsx-to-sheets-in-google-drive-api-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!