使用Google App Engine如何在谷歌文档中上传文档(python)

时间:2010-06-21 07:08:17

标签: python google-app-engine google-docs-api

我想使用Google Apps Engine(python)将文档,文件上传到Google文档

任何代码或链接都将受到赞赏

2 个答案:

答案 0 :(得分:3)

请参阅the documentation,,但您可以尝试以下方式:

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 :(得分: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 doc