xlsxwriter错误:AttributeError:“工作簿"对象没有属性"add_format" [英] xlsxwriter error: AttributeError: 'Workbook' object has no attribute 'add_format'

查看:461
本文介绍了xlsxwriter错误:AttributeError:“工作簿"对象没有属性"add_format"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xlsxwriter做一些简单的条件格式设置,但是当我运行下面的代码时出现此错误.

I'm doing some simple conditional formatting using xlsxwriter but I am getting this error when I run the code below.

AttributeError: 'Workbook' object has no attribute 'add_format'

我已经更新了xlsxwriter,并查看了许多有关SO和文档的问题,但仍然没有任何效果.

I have updated xlsxwriter and looked at a lot of questions on SO and documentation but nothing has worked yet.

这是我的代码:

workbook = load_workbook(input_excel_filename)
writer = pd.ExcelWriter(input_excel_filename, engine="xlsxwriter")
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

trends_sheet = writer.sheets["Trends"]
slight_increase = writer.book.add_format({"bg_color":"#d3e6d5"})
trends_sheet.conditional_format("E:E", {"type":"cell", "criteria":"==", "value":"Slight Increase", "format":slight_increase})

推荐答案

部分问题是我需要显式设置 writer.book .因此,在定义 writer 之后添加 writer.book = workbook 行.还将 engine ="openpyxl" 添加到ExcelWriter消除了后续错误.总体来说,这似乎可行:

Part of the problem was I needed to set writer.book explicitly. So add the line writer.book = workbook after defining writer. Also adding engine="openpyxl" to the ExcelWriter got rid of a subsequent error. Altogether this seems to work:

workbook = load_workbook(input_excel_filename)
writer = pd.ExcelWriter(input_excel_filename, engine="openpyxl")
writer.book = workbook
writer.sheets = dict((ws.title, ws) for ws in wb.worksheets)
data.to_excel(writer, sheet_name="Data", index=False)
writer.save()
writer.close()

我无法使它与条件格式一起使用,但是直接在Excel电子表格中设置格式似乎确实可行,因为即使重写了数据,格式仍保持不变.

I couldn't get it to work with conditional formatting but setting formatting in the Excel spreadsheet directly actually seems to work, because even if the data is rewritten the formatting stays intact.

这篇关于xlsxwriter错误:AttributeError:“工作簿"对象没有属性"add_format"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆