我想將我的代碼上傳到Google雲端硬盤時自動將xlsx文件轉換爲Google電子表格。然而,雖然轉換成功爲csv文件,我得到:如何在Google Drive API v3中將XLSX轉換爲表格
<HttpError 400 when requesting https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&alt=json returned "Bad Request">
當試圖上傳xlsx。
這裏是我的代碼:
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")
我怎樣才能做到這一點的V3?在v2中如何做到這一點非常明確,但不是在更新後的API中。
這對我有效! – Elliptica