xlsxwriter模块无法正确打开/关闭Excel文件

spe*_*ekr 4 python error-handling exception-handling exception xlsxwriter

我正在编写一个程序,使用该xlsxwriter模块将数据写入Excel文件.

打开工作簿的代码是:

excel = xlsxwriter.Workbook('stock.xlsx')
Run Code Online (Sandbox Code Playgroud)

这曾经工作过.然后我在程序的底部更改了一些东西(在该行之后waaaaay),现在它不起作用,这样说:

Exception ignored in: <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object at 0x02C702B0>>
Traceback (most recent call last):
  File "c:\python34\lib\site-packages\xlsxwriter\workbook.py", line 147, in __del__
    raise Exception("Exception caught in workbook destructor. "
Exception: Exception caught in workbook destructor. Explicit close() may be required for workbook.
Run Code Online (Sandbox Code Playgroud)

这种情况发生在我再次运行它之前忘记关闭文件时(因为它试图写入在Excel中打开但无法正常工作的文件),但我甚至没有打开Excel,它就是这样做的.

我怎样才能解决这个问题?我需要重启还是什么?

此外,try...except如果初始化不起作用,我试图有一个循环来停止程序.即使except:只有,没有特定的例外,它仍然完成程序,除非我手动杀死它.该脚本基本上打开Excel文件,花费很长时间从Internet下载数据,然后将其写入Excel文件.如果初始化不起作用,我希望它停止,所以我不必等待脚本完成(最多可能需要15分钟).我很确定它与它说" Exception ignored" 的事实有关,但我不熟悉Python中的所有错误.

编辑:

excel.close()在最后添加了一个命令,现在它没有给我第一个错误,但是第二个错误(更大更可怕):

Traceback (most recent call last):
  File "C:\Users\carte_000\Python\stock_get_rev8.py", line 161, in <module>
    excel.close()
  File "c:\python34\lib\site-packages\xlsxwriter\workbook.py", line 287, in close
    self._store_workbook()
  File "c:\python34\lib\site-packages\xlsxwriter\workbook.py", line 510, in _store_workbook
    xml_files = packager._create_package()
  File "c:\python34\lib\site-packages\xlsxwriter\packager.py", line 132, in _create_package
    self._write_worksheet_files()
  File "c:\python34\lib\site-packages\xlsxwriter\packager.py", line 189, in _write_worksheet_files
    worksheet._assemble_xml_file()
  File "c:\python34\lib\site-packages\xlsxwriter\worksheet.py", line 3395, in _assemble_xml_file
    self._write_sheet_data()
  File "c:\python34\lib\site-packages\xlsxwriter\worksheet.py", line 4802, in _write_sheet_data
    self._write_rows()
  File "c:\python34\lib\site-packages\xlsxwriter\worksheet.py", line 4988, in _write_rows
    self._write_cell(row_num, col_num, col_ref)
  File "c:\python34\lib\site-packages\xlsxwriter\worksheet.py", line 5148, in _write_cell
    xf_index = cell.format._get_xf_index()
AttributeError: type object 'str' has no attribute '_get_xf_index'
Run Code Online (Sandbox Code Playgroud)

编辑2:

实际写入文件的代码部分是:

for r, row in enumerate(data):
    for c, cell in enumerate(row):
        if 'percent' in formats[c]:
            sheet.write(r + r_offset, c + c_offset, cell, eval(formats[c].replace('_f', '')))
        elif '_f' in formats[c]:
            sheet.write(r + r_offset, c + c_offset, cell.format(n=str(r + r_offset)), eval(formats[c].replace('_f', '')))
        else:
            sheet.write(r + r_offset, c + c_offset, cell, eval(formats[c][0] + formats[c].replace('_f', '')[-1]))
Run Code Online (Sandbox Code Playgroud)

如果我if...else用一个替换所有花哨的东西

sheet.write(r + r_offset, c + c_offset, cell)
Run Code Online (Sandbox Code Playgroud)

它没有给我错误,似乎工作正常.

这不提供我需要的功能,因为有些列需要是基于行更改数字的公式.上面的代码是什么导致excel.close()行错误?

spe*_*ekr 6

在我的代码中,我试图引用一种不存在的格式.当我打电话时excel.close(),这似乎是它实际上写了所有内容,所以如果格式无效,它会在那里抛出一个错误.