python:将数据写入excel文件但是"无法关闭文件"?

时间:2017-12-07 00:27:17

标签: python excel pyexcelerate

所以我有这个pandas数据表,我希望写入excel工作簿。 我正在使用pyexcelerate包。 这是我的代码:

from pyexcelerate import Workbook

def df_to_excel(df, path, sheet_name='Sheet 1'):
    data = [df.columns.tolist(), ] + df.values.tolist()
    wb = Workbook()
    wb.new_sheet(sheet_name, data=data)
    wb.save(path)

df_to_excel(df=MergedDataAll, path=merged_file)

在我使用相同代码之前,我没有遇到此问题;但今天这个错误不断出现:

Traceback (most recent call last):
  File "C:/Users/y/Documents/python codes/DataMerging.py", line 71, in <module>
    df_to_excel(df=MergedDataAll, path=merged_file)
  File "C:/Users/y/Documents/python codes/DataMerging.py", line 68, in df_to_excel
    wb.save(path)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Workbook.py", line 82, in save
    self._save(fp)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Workbook.py", line 78, in _save
    self._writer.save(file_handle)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Writer.py", line 58, in save
    for s in sheetStream:
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\jinja2\environment.py", line 1045, in generate
    yield self.environment.handle_exception(exc_info, True)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\jinja2\environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\templates\xl\worksheets\sheet.xml", line 11, in top-level template code
    <sheetData>{% for x, row in worksheet.get_xml_data() %}{{ worksheet.get_row_xml_string(x) }}{% for cell in row %}{{ cell }}{% endfor %}</row>{% endfor %}</sheetData>
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Worksheet.py", line 265, in get_xml_data
    row_data.append(self.__get_cell_data(cell, x, y, style))
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Worksheet.py", line 188, in __get_cell_data
    z = '"><v>%s</v></c>' % (DataTypes.to_excel_date(cell))
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\DataTypes.py", line 57, in to_excel_date
    excel_date = delta.days + (float(delta.seconds) + float(delta.microseconds) / 1E6) / (60 * 60 * 24) + 1
AttributeError: 'NaTType' object has no attribute 'days'
Exception ignored in: <bound method ZipFile.__del__ of <zipfile.ZipFile [closed]>>
Traceback (most recent call last):
  File "C:\Users\y\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1663, in __del__
  File "C:\Users\y\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1680, in close
ValueError: seek of closed file

我尝试使用谷歌搜索,并使用类似于

的东西
wd.close()
wd._archive_.close()

但两者都没用。

这里有一些我已经研究过的参考文章:

Openpyxl does not close Excel workbook in read only mode

Python to close a workbook using win32com

Closing files in openpyxl

https://github.com/python-pillow/Pillow/issues/1630

1 个答案:

答案 0 :(得分:0)

我终于明白了!所以我想我也可以分享我的发现。

答案来自这篇文章:

'NaTType' object has no attribute 'days'

基本上升级pandas库解决了这个问题......

pip install --upgrade pandas

特别感谢Blckknght指出NaTType问题;如果他没有指出,我不会更多地考虑这个问题。