如何在Google App Engine中使用Python上传文档到Google Docs
我想用Google Apps Engine(Python)把文档或文件上传到谷歌文档。
如果有代码或者链接的话,我会很感激。
2 个回答
3
可以查看这个文档,不过你可以试试下面的代码:
ms = gdata.MediaSource(file_path='/path/to/your/test.doc', content_type=gdata.docs.service.SUPPORTED_FILETYPES['DOC'])
entry = gd_client.Upload(ms, 'MyDocTitle')
print 'Document now accessible online at:', entry.GetAlternateLink().href
1
解决方案是通过文件上传,你需要用下面这行代码在Python中读取数据:
这是一个用来读取文件大小的函数
def getSize(self,fileobject):
fileobject.seek(0,2) # move the cursor to the end of the file
size = fileobject.tell()
return size
f = self.request.POST.get('fname').file
media = gdata.data.MediaSource(file_handle=f.read(), content_type=gdata.docs.service.SUPPORTED_FILETYPES[ext], content_length=self.getSize(self.request.POST.get('fname').file))
同时,你还需要修改Google的gdata Python库来实现这个功能:
在client.py文件中:
找到
def upload_file然后把:
while not entry:
entry = self.upload_chunk(start_byte, self.file_handle.read(self.chunk_size))
start_byte += self.chunk_size
替换成:
while not entry:
entry = self.upload_chunk(start_byte, self.file_handle)
start_byte += self.chunk_size
这样你就可以把文件目录上传到Google文档了