在python-3.3中使用pandas.ExcelWriter.to_excel时出错

时间:2015-06-24 17:36:21

标签: python excel pandas

我在python-3.3中使用Pandas版本0.16.2和openpyxl版本2.2.4。我正在尝试编写一个包含浮点数,字符串和NaN的非常简单的pandas数据帧。当我使用代码时:

import pandas as pd
writer = pd.ExcelWriter(xls_path)
df.to_excel(writer,'sheet1')
writer.save()

我收到错误:

TypeError                                 Traceback (most recent call last)

<ipython-input-3-2349bf9de8ec> in save_xls(list_dfs, xls_path, sheetnames)
      5                  import pandas as pd
      6                  writer = pd.ExcelWriter(xls_path)
----> 7             df.to_excel(writer,sheetnames[n])
      8 
      9     writer.save()

/Users/dylan/Virtualenvs/ve33/lib/python3.3/site-packages/pandas/core/frame.py in to_excel(self,     excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow,     startcol, engine, merge_cells, encoding, inf_rep)
   1272         formatted_cells = formatter.get_formatted_cells()
   1273         excel_writer.write_cells(formatted_cells, sheet_name,
-> 1274                                  startrow=startrow, startcol=startcol)
   1275         if need_save:
   1276             excel_writer.save()

/Users/dylan/Virtualenvs/ve33/lib/python3.3/site-packages/pandas/io/excel.py in write_cells(self,     cells, sheet_name, startrow, startcol)
    776 
    777             if style_kwargs:
--> 778                 xcell.style = xcell.style.copy(**style_kwargs)
    779 
    780             if cell.mergestart is not None and cell.mergeend is not None:

/Users/dylan/Virtualenvs/ve33/lib/python3.3/site-packages/openpyxl/compat/__init__.py in     new_func(*args, **kwargs)
     65                 lineno=_code.co_firstlineno + 1
     66             )
---> 67             return obj(*args, **kwargs)
     68         return new_func
     69 

TypeError: copy() got an unexpected keyword argument 'font'    

对可能出错的任何帮助?

谢谢!

1 个答案:

答案 0 :(得分:1)

您能提供重现错误的方法吗?我使用Python 3.4,pandas 0.16.2和excel编写器引擎openpyxl。以下示例代码在我的PC上完美运行。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(1000, 3))

xlsx_path = 'my_excel_file.xlsx'
writer = pd.ExcelWriter(xlsx_path)
df.to_excel(writer, 'Sheet1', engine='openpyxl')