【问题标题】:How to highlight rows based on content in Excel Dataframe?如何根据 Excel Dataframe 中的内容突出显示行?
【发布时间】:2020-07-21 08:31:20
【问题描述】:

我有一个包含左侧数据的 excel 文件,我正在尝试对其进行格式化以获得 将数据格式化为右侧的表格。

使用我当前的代码,我可以格式化所有包含标题的行(H1、H2、...)

这是file.xlsx的内容:

这是我当前的代码:

import pandas as pd
import numpy as np

from xlsxwriter.utility import xl_rowcol_to_cell

data = {'H1': {0: 'A', 1: '', 2: 'H1', 3: 'A', 4: '', 5: 'H1', 6: 'A', 7: 'A', 8: 'B', 9: 'B', 10: 'B', 11: '', 12: 'H1', 13: 'B', 14: 'B', 15: '', 16: 'H1', 17: 'C', 18: 'C', 19: 'C', 20: 'D', 21: 'D', 22: ''}, 'H2': {0: 'Rty', 1: '', 2: 'H2', 3: 'Rty', 4: '', 5: 'H2', 6: 'Rty', 7: 'Rty', 8: 'Rty', 9: 'Rty', 10: 'Rty', 11: '', 12: 'H2', 13: 'Rty', 14: 'Rty', 15: '', 16: 'H2', 17: 'Rty', 18: 'Rty', 19: 'Rty', 20: 'Rty', 21: 'Rty', 22: ''}, 'H3': {0: '1195', 1: '', 2: 'H3', 3: '1195', 4: '', 5: 'H3', 6: '1195', 7: '1195', 8: '1195', 9: '1195', 10: '1195', 11: '', 12: 'H3', 13: '1195', 14: '1195', 15: '', 16: 'H3', 17: '1195', 18: '1195', 19: '1195', 20: '1195', 21: '1195', 22: ''}, 'H4': {0: '9038', 1: 'H3=9038, 000', 2: 'H4', 3: '1355', 4: 'H3=1355, 363', 5: 'H4', 6: '2022', 7: '2022', 8: '2022', 9: '2022', 10: '2022', 11: 'H3=2022, 234', 12: 'H4', 13: '2564', 14: '2564', 15: 'H3=2564, 726', 16: 'H4', 17: '1501', 18: '1501', 19: '1501', 20: '1501', 21: '1501', 22: 'H3=1501, 143'}, 'H5': {0: '1537', 1: '', 2: 'H5', 3: '8', 4: '', 5: 'H5', 6: '59', 7: '78', 8: '76', 9: '6', 10: '31', 11: '', 12: 'H5', 13: '71', 14: '17', 15: '', 16: 'H5', 17: '72', 18: '89', 19: '47', 20: '32', 21: '233', 22: ''}}
df = pd.DataFrame.from_dict(data)


writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
df.to_excel(writer, index=False, sheet_name='Output')

workbook = writer.book
worksheet = writer.sheets['Output']
number_rows = len(df.index)

format1 = workbook.add_format({'bg_color': 'black', 'font_color': 'yellow'})

for r in range(0,number_rows):
    if df.iat[r,0] == "H1":
        worksheet.set_row(r+1, None, format1) 

writer.save()

这是我当前的输出:

我被困在如何限制从 A 列到 E 列的格式以及如何根据不同的情况将颜色嵌入绿色、黄色、绿色黄色 当 A 列中的值发生变化时。我的意思是,对于 A 列中的所有连续值 =“A”以绿色突出显示,当突出显示变为黄色时 当高亮再次变为绿色等等。

我该怎么做?提前致谢。

【问题讨论】:

  • 首先,我建议使用 python win32com 模块来控制 Excel,然后弄清楚如何自动化 Excel 的最佳方法是在 Excel 中记录你想要的 VBA 宏,这将“大部分”让您了解在 python 中使用的方法。以How to create a pivot table in Excel with python win32com 为例。
  • 感谢您的回答。你的意思是,做一个适合我想要的 VBA 宏。然后使用 win32com 在 python 中复制相同的步骤/命令?
  • 是的,我就是这个意思。我链接到的答案中有一个 Jupyter 笔记本链接。它有例子
  • 非常感谢特伦顿,我会检查您共享的链接。最后一个问题。我的实际数据在一个列表中,然后我使用 Pandas Dataframe 和StyleFrame.ExcelWriter 将其保存在一个 excel 文件中。使用 win32com 我可以从 python 列表中获取该输入,通过 win32com 命令应用“VBA 宏”并将其保存在 excel 文件中吗?还是我需要保存文件,一旦我有了 output.xlsx,就拿那个 Excel 文件并用 win32com 修改它的内容?
  • 应用 VBA 宏 使用 win32com,你没有应用 Excel 宏。 win32com 公开了所有的 Excel 方法。然后,您使用这些方法编写 python 代码。 win32com 的好处是所有可能的方法都暴露出来了。大多数 python 模块似乎无法访问 Excel 的所有功能。录制宏只会告诉您使用哪种方法来实现特定功能。

标签: python-3.x pandas dataframe export-to-excel


【解决方案1】:

你可以用不同的excel库来做,比如openpyxl

您可以分别格式化每个单元格 例如:

from openpyxl import Workbook
from openpyxl.styles import Font, Color, colors, fills
from openpyxl.utils.dataframe import dataframe_to_rows
wb = Workbook()
ws = wb.active

for r in dataframe_to_rows(df, index=False, header=True):
    ws.append(r)

a1 = ws['A1']
a1.font = Font(color="FF0000")
a1.fill = fills.PatternFill(patternType='solid', fgColor=Color(rgb='00FF00'))
wb.save("pandas_openpyxl.xlsx")

他们在这里有很好的文档:https://openpyxl.readthedocs.io/en/stable/pandas.html

【讨论】:

  • 非常感谢您的帮助。我仍在弄清楚如何实现我的目标,但您的选择似乎有另一种好方法,我能够以比我之前的尝试更简单的方式突出显示至少包含“H1,H2 ..”的行。跨度>
  • 非常感谢,通过您的示例和您分享的链接,经过多次测试,我能够使用您建议的库 openpyxl 获得所需的结果。
【解决方案2】:

如果您想继续使用xlsxwriter Python 模块,您可以使用工作表对象上的write(…) 方法一步设置单元格的内容和格式。

您必须分解您的 to_excel() 方法并在循环中单独写入每个 DataFrame 值。

示例单元格创建和格式化调用:

cell_format = workbook.add_format({'bold': True, 'italic': True})

# inside a loop iterating over your DataFrame
worksheet.write(row, column, value, cell_format)  # Cell is bold and italic.

【讨论】:

  • 感谢 JimmyNJ 的其他方法。我知道我想如何格式化工作表内容有点复杂,但是例如,您将如何格式化第一列中所有具有“A”的行?
  • @GerCas :您必须遍历单元格值并将第一个字符与“A”进行比较,然后您可以再次调用 write(...)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2015-04-21
相关资源
最近更新 更多