使用xlwt在Google App Engine中写Excel文件 - 出现IOError: invalid mode: wb

0 投票
1 回答
908 浏览
提问于 2025-04-16 21:30

在我的应用程序中,我正在尝试使用xlwt这个工具把数据写入Excel文件。当我运行GAE应用时,出现了以下错误。请帮我解决这个问题。

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 700, in __call__
    handler.get(*groups)
  File "C:\Users\stocklist\temp.py", line 39, in get
    wb.save('example.xls')
  File "C:\Users\stocklist\xlwt\Workbook.py", line 634, in save
    doc.save(filename, self.get_biff_data())
  File "C:\Users\stocklist\xlwt\CompoundDoc.py", line 507, in save
    f = open(file_name_or_filelike_obj, 'wb')
  File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 1427, in __init__
    raise IOError('invalid mode: %s' % mode)
IOError: invalid mode: wb

源代码:

class MainHandler(webapp.RequestHandler):
    def get(self):      

        font0 = xlwt.Font()
        font0.name = 'Times New Roman'
        font0.colour_index = 2
        font0.bold = True

        style0 = xlwt.XFStyle()
        style0.font = font0

        style1 = xlwt.XFStyle()
        style1.num_format_str = 'D-MMM-YY'

        wb = xlwt.Workbook()
        ws = wb.add_sheet('A Test Sheet')

        ws.write(0, 0, 'Test', style0)
        ws.write(1, 0, datetime.now(), style1)
        ws.write(2, 0, 1)
        ws.write(2, 1, 1)
        ws.write(2, 2, xlwt.Formula("A3+B3"))

        wb.save('example.xls')

1 个回答

3

在App Engine上,你不能直接把文件写入文件系统。相反,你需要把文件转换成一种可以存储的格式,然后把它保存在数据存储区或者大对象存储区里。

撰写回答