Pandas:无法写入excel文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30102232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:19:28  来源:igfitidea点击:

Pandas: can not write to excel file

pythonpandas

提问by Tengis

Trying this example from the documentation

文档中尝试此示例

writer = ExcelWriter('output.xlsx')
df1.to_excel(writer,'Sheet1')
df2.to_excel(writer,'Sheet2')
writer.save()

I found out that I can not write to an excel file with the error

我发现我无法写入带有错误的 excel 文件

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

I'm using Panda 0.16 on a Mac pro.

我在 Mac pro 上使用 Panda 0.16。

EDIT: writing to an xls file worked just fine. I do not insist in having an xlsx file, just wonder why it does not work.

编辑:写入 xls 文件工作得很好。我不坚持有一个 xlsx 文件,只是想知道为什么它不起作用。

采纳答案by Tal Peretz

For fast solution replace this

对于快速解决方案替换这个

xcell.style = xcell.style.copy(**style_kwargs)

with

pass

At pandas/io/excel.py line 778.

在 pandas/io/excel.py 第 778 行。

openpyxl upgraded their api and pandas also need to be updated for support openpyxl.

openpyxl 升级了他们的 api,pandas 也需要更新以支持 openpyxl。

回答by pbamba

As per their documentation, pandas depends

根据他们的文档,pandas 取决于

on openpyxl version 1.6.1 or higher, but lower than 2.0.0

在 openpyxl 版本 1.6.1 或更高,但低于 2.0.0

The last openpyxlversion lower than 2.0.0 being version 1.8.6, you should simply remove your current openpyxl version and run:

最后一个低于 2.0.0 的openpyxl版本是 1.8.6 版本,您应该简单地删除当前的 openpyxl 版本并运行:

pip install openpyxl==1.8.6

if your using pip, or find an equivalent way to install this specific version.

如果您使用 pip,或者找到安装此特定版本的等效方法。

回答by slushy

If you don't care whether the headers have borders around them and bold font, and you don't want to restrict the version of openpyxl, the quickest way is to overwrite the header_styledictionary to be None.

如果你不关心标题周围是否有边框和粗体,并且你不想限制 openpyxl 的版本,最快的方法是将header_style字典覆盖为None.

If you also have dates or datetimes, you must also explicitly set the workbook's dateand datetimeformats to None:

如果您还有日期或日期时间,则还必须将工作簿的datedatetime格式显式设置为None

from datetime import datetime
import pandas as pd
pd.core.format.header_style = None  # <--- Workaround for header formatting

dt = datetime.now()
d = datetime.date(datetime.now())
df1 = pd.DataFrame([{'c1': 'alpha', 'c2': 1}, {'c1': 'beta', 'c2': 2}])
df2 = pd.DataFrame([{'c1': dt, 'c2': d}, {'c1': dt, 'c2': d}])

with pd.ExcelWriter('output.xlsx') as writer:
    writer.date_format = None # <--- Workaround for date formatting
    writer.datetime_format = None  # <--- this one for datetime
    df1.to_excel(writer,'Sheet1')
    df2.to_excel(writer,'Sheet2')

Not clear why the keyword arguments never make it through the openpyxl deprecation wrapper...but they don't. In addition if you're formatting any other cells use the new openpyxl api.

不清楚为什么关键字参数永远不会通过 openpyxl 弃用包装器......但他们没有。此外,如果您要格式化任何其他单元格,请使用新的openpyxl api

All of these problems go away if you have Anaconda, by the way.

顺便说一下,如果你有 Anaconda,所有这些问题都会消失。

回答by Naor Zakk

An update to pandas should slove this.

大Pandas的更新应该解决这个问题。

pip install -U pandas

It works for pandas version 0.17+

它适用于Pandas版本 0.17+