我正在尝试将数据导出到应用了条件格式的excel文件中:
import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles.differential import DifferentialStyle
from openpyxl.formatting.rule import ColorScaleRule, CellIsRule, FormulaRule
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
wb = writer.book
ws = writer.sheets['Sheet1']
ws.conditional_formatting.add('A1:A10',
ColorScaleRule(start_type='min', start_color='AA0000',
end_type='max', end_color='00AA00')
)
worksheet.conditional_formatting.add("O2:O295", color_scale_rule)
workbook.save(filename="sample_conditional_formatting_color_scale.xlsx")
我收到以下错误:
AttributeError: 'Worksheet' object has no attribute 'conditional_formatting'
我的目标是:
1条答案
按热度按时间5ktev3wc1#
您创建的Dataframe不正确。
输出:
调整色阶以匹配预期输出是留给读者的练习。