Xlsxwriter module will not open / close Excel file

I am writing a program that writes data to an Excel file using a module xlsxwriter.

Code that opens a workbook:

excel = xlsxwriter.Workbook('stock.xlsx')

It worked. Then I changed some things at the bottom of the program (waaaaay after this line), and now it will not work, saying the following:

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.

This happened when I forgot to close the file before restarting it (since it is trying to write a file that opens in Excel that will not work), but I don’t even have Excel open, and it does.

How can i fix this? Do I need to reboot or something else?

, try...except, , . except: , - , - , . script Excel, , Excel. , , , script ( 15 ). , , "Exception ignored", fu Python.

EDIT:

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'

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]))

if...else

sheet.write(r + r_offset, c + c_offset, cell)

, , .

, , , . excel.close()?

+4
4

. excel.close(), , , , , .

+6

AttributeError: type object 'str' has no attribute '_get_xf_index'

, sheet.write() sheet.write(row, col, "Some_text", variable) sheet.write(row, col, "Some_text"+variable)

... excel.close(), script excel

+3

, . , ,

excel.close()

docs .

+1

excel.close()?

excel.close(), . sheet.write: eval xlsxwriter.Format, str , _get_xf_index, .

You can improve your code by avoiding use eval, which is almost always bad practice, matching your formats in dicts and / or lists, or, if the formats are really complex, creating a function that returns the format.

+1
source

All Articles