问题描述
我想将我的代码上传到Google云端硬盘时,将xlsx文件自动转换为Google电子表格。然而,虽然转换成功的csv文件,我得到:
< HttpError 400请求https:// www时。 googleapis.com/upload/drive/v3/files?uploadType=resumable&alt=json返回了错误的请求>
当试图上传xlsx时。
这里是我的代码:
def upload_service(filepath,name =,description =,fileID =,parentID =):
使用资源(服务)对象上传文件到驱动器。
if service ==:authenticate_service()
if name ==:
name = str(os.path.basename(filepath).split(os.extsep)[0])#从文件路径获取
extension = str(os.path.basename(filepath).split(os.extsep)[1])lower()
if extension ==csv:#CSV
mime_type =text / csv
[xls,xlsx]中的elif扩展名:#EXCEL
mime_type =application / ms-excel
else:
return
$ b $ media_body = MediaFileUpload(filepath,mimetype = mime_type,resumable = True)
如果parentID ==:
meta = dict(name = name,mimeType =application / vnd.google-apps.spreadsheet,desc ription = description)
else:
meta = dict(name = name,mimeType =application / vnd.google-apps.spreadsheet,description = description,parents = [parentID])
if fileID ==:#CREATE $ b $ 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中。
在APIv3中,您需要指定一个非常特定的 MIME类型,以便进行转换。
在,您会注意到支持的转化可以在关于资源的 importFormats
数组。使用
GET https://www.googleapis.com获取
importFormats
列表/ drive / v3 / about?fields = importFormats& key = {YOUR_API_KEY}
或转到并输入 importFormats
您会在响应中注意到:
application / vnd.ms-excel:[
application / vnd.google-apps.spreadsheet
]
在您的代码中,使用:
elif extension in [xls,xlsx]:#EXCEL
pre>
mime_type =application / vnd.ms-excel
(注意额外的
vnd。
),它应该很好运作!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.
解决方案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 theimportFormats
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!这篇关于如何在Google Drive API v3中将XLSX转换为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!