2017-12-07 24 views
0

だから私はこのパンダのデータテーブルをExcelワークブックに書きたいと思っています。 私はpyexcelerateパッケージを使用しています。 は、ここに私のコードです:python:ファイルをExcelに書き込むのに "ファイルを閉じることができません"というデータを書き込んでいますか?

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

に似たものを使用しますが、両方は有用ではありません。私はに見てきた

here'reいくつかの参考記事:

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

+0

私はあなたがそれを後ろ向きに持っていると思います。あなたは既に閉じられているファイルを探しています。 'merged_file'とは何ですか?この関数を入力する前に閉じていますか? – Shadow

+0

私は*特定の*例外を正しく読んでいますが、私はあなたが遭遇している最初の "本当の"エラーがzipファイルシークのものではなく、むしろ 'AttributeError: 'NaTType'コードが日付を書き出しようとしている間に「days」と表示されます。あなたはあなたのデータをチェックして、日付がどこにあるべきか(それが他の何かを意味しても日付と間違っているかもしれない)偽の価値を持っていないことを確認できますか? – Blckknght

+0

@Shadow merged_fileは、ファイルの名前へのファイルパスです。私はこのファイルを書いています。 – alwaysaskingquestions

答えて

0

は、私はついにそれを考え出しました!だから私は自分の発見を分かち合うと思った。

答えはこの記事からでした:

'NaTType' object has no attribute 'days'

は、基本的にこの問題を解決パンダライブラリをアップグレード... NaTTypeの問題を指摘してBlckknghtへ

pip install --upgrade pandas 

感謝。彼がそれを指摘しなかったなら、私はこれ以上のことを見ていないでしょう。

関連する問題