简体   繁体   English

我如何存储使用pyExcelerator创建的Excel文件作为db.BlobProperty()的输入

[英]How can i store excel file created using pyExcelerator as input for db.BlobProperty()

How can i store excel file created using pyExcelerator as input for db.BlobProperty() ? 我如何存储使用pyExcelerator创建的excel文件作为db.BlobProperty()的输入?

Actally what i need is that Using taskqueue program will create a excel file and store it in the datastore. 实际上,我需要的是使用Taskqueue程序将创建一个excel文件并将其存储在数据存储区中。 And will send a link to the users to download the file. 并将向用户发送链接以下载文件。 How do i do this ? 我该怎么做呢 ? Please help me 请帮我

Datamodel: 资料模型:

class filestore(db.Model):
   stock_file = db.BlobProperty()

Python code for storing the excel file in datastore 用于将Excel文件存储在数据存储区中的Python代码

from pyExcelerator import *
class MainHandler(webapp.RequestHandler):
  def get(self):                  
     w = Workbook()
     ws = w.add_sheet('Hey, Dude')
     ws.write(0, 0, 'Part Number')        
     self.response.headers['Content-Type'] = 'application/ms-excel'
     self.response.headers['Content-Transfer-Encoding'] = 'Binary'
     self.response.headers['Content-disposition'] = 'attachment; filename="Test.xls"'

     temp_file = filestore()
     temp_file.stock_file = db.blob(wb.save(self.response.out)) // Storing 0kb file 
     temp_file.put()

After inserting new file, file size in 0kb why ? 插入新文件后,为什么文件大小为0kb?

If your goal is to create an Excel file and save it as a blob for later use you need to first save it as a StringIO object, not construct a response, as you are doing currently. 如果您的目标是创建一个Excel文件并将其另存为Blob供以后使用,则需要先将其另存为StringIO对象,而不是像现在那样构造一个响应。

Here is a sample using xlwt (a fork of pyExcelerator): 这是使用xlwt(pyExcelerator的一个分支)的示例:

import xlwt

file_type = 'application/ms-excel'
file_name = 'sample.xls'

wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Hey, Dude')
sheet.write(0, 0, 'Part Number')

file = StringIO.StringIO()
wbk.save(file)
file.seek(0)    

with files.open(file_name, 'a') as f:
  f.write('%s' % file.getvalue())

files.finalize(file_name)

blob_key = files.blobstore.get_blob_key(file_name)

This code will save the file as blobproperty() in datastore. 此代码会将文件另存为数据存储中的blobproperty()。

    import StringIO

    w = Workbook()
    ws = w.add_sheet('Hey, Dude')
    ws.write(0, 0, 'Part Number')

    buffer = StringIO.StringIO()
    w.save(buffer)
    contents = buffer.getvalue()
    mymodel = filestore()
    mymodel.stock_file = db.Blob(contents)
    mymodel.put()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从许多实体中删除db.BlobProperty? - How to delete a db.BlobProperty from many entities? pyexcelerator:如何通过工作表名称删除Excel工作表 - pyexcelerator: how to delete a excel-sheet by sheet-name 使用pyExcelerator / xlrd进行数据透视 - pivots using pyExcelerator/xlrd 如何使用pyexcelerator的xlwt util为整个行着色 - How to color the entire row using xlwt util of pyexcelerator 我创建了一个 python 脚本将 excel 表导入我的数据库,但现在我想创建将文件路径作为输入的 Gui - I've created a python script to import excel sheets into my DB, But now i want create Gui which will take file path as input 如何在 python 中将多个文件作为输入并存储在变量中 - How can I take multiple file as input in python & store in a variable 如何使用js验证输入并将输入存储在db中 - How to validate input using js and store the input in db 如何存储从用户输入创建的变量并在 Python 的不同文件中使用它? - How do I store a variable that has been created from a user input and use it in a different file in Python? 使用pyExcelerator使用Django生成动态Excel文件。 确保唯一临时文件名 - Use pyExcelerator to generate dynamic Excel file with Django. Ensure unique temporary filename 我如何知道 os.makedirs() 创建的文件的路径并将其存储在变量中供以后使用? - how can i know the path of a file created by os.makedirs() and store it in a variable for later use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM