[英]How to highlight an Excel row when there is a string value in a cell on a row using Openpyxl?
當“新記錄 ID”列下的單元格中有一個值時,我試圖突出顯示電子表格中的一行(從我的 split_values 數據框生成)該行的絕對引用是“J”。
以下是我最近的嘗試:
# Group entries by client name and create team spreadsheet
split_values = submitted_and_revised['Client Name'].unique()
print(split_values)
for value in split_values:
teams = submitted_and_revised[submitted_and_revised['Client Name'] == value]
output_file_name = "Team_" + str(value) + ".xlsx"
from openpyxl import Workbook
from openpyxl.styles import numbers, PatternFill, colors
from openpyxl.utils import get_column_letter
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.styles import Alignment
from openpyxl.formatting.rule import Rule
from openpyxl.styles.differential import DifferentialStyle
workbook = Workbook()
sheet = workbook.active
############################################################
# This section works in this location, but I can't figure out how to make it conditional on the New Record ID in rule.formula . I'm also not sure what the sheet.conditional_formating.add("A1:0100", rule) means...
red_background = PatternFill(bgColor=colors.RED)
diff_style = DifferentialStyle(fill=red_background)
rule = Rule(type="expression", dxf=diff_style)
rule.formula = ["$J1==working_revised['New Record ID']"]
sheet.conditional_formatting.add("A1:O100", rule)
############################################################
#Format Column Widths
#sheet.column_dimensions['C'].auto_size = True
sheet.column_dimensions['B'].width = float(18)
sheet.column_dimensions['C'].width = float(5)
sheet.column_dimensions['C'].width = float(25.25)
sheet.column_dimensions['D'].width = float(20)
sheet.column_dimensions['E'].width = float(6)
sheet.column_dimensions['G'].width = float(65)
sheet.column_dimensions['H'].width = float(20)
sheet.column_dimensions['I'].width = float(14)
sheet.column_dimensions['J'].width = float(14)
sheet.column_dimensions['K'].width = float(50)
for row in dataframe_to_rows(teams, index=False, header=True):
sheet.append(row)
for rows in sheet.iter_rows(min_row=1, max_row=None, min_col=None, max_col=None):
for cell in rows:
cell.alignment = Alignment(vertical='center',wrapText=True)
workbook.save(output_file_name)
提前感謝您提供的任何幫助!
謝謝 Dror Av。 感謝您在這個問題上提供的所有支持。 我無法得到你的答案。 一般來說,我對編碼仍然非常陌生,而且我確信我錯過了一些簡單的東西。 我玩了代碼,終於找到了下面的解決方案。
它不能完美運行,因為除了突出顯示的“新記錄 ID”行之外,它還突出顯示 header 行。 盡管我試圖修復那塊,但我沒能做到。 對於它的價值,這是我想出的解決方案:
# Import needed modules
from openpyxl import Workbook
from openpyxl.formatting.rule import Rule
from openpyxl.styles.differential import DifferentialStyle
from openpyxl.styles import Font, PatternFill, colors, Alignment
from openpyxl.utils import get_column_letter
from openpyxl.utils.dataframe import dataframe_to_rows
#Group by unique clients
split_values = submitted_and_revised['Client Name'].unique()
print(split_values)
for value in split_values:
teams = submitted_and_revised[submitted_and_revised['Client Name'] == value]
output_file_name = "Team_" + str(value) + ".xlsx"
workbook = Workbook()
sheet = workbook.active
#Format Column Widths
#sheet.column_dimensions['C'].auto_size = True
sheet.column_dimensions['A'].width = float(3)
sheet.column_dimensions['B'].width = float(18)
sheet.column_dimensions['C'].width = float(25)
sheet.column_dimensions['D'].width = float(20)
sheet.column_dimensions['E'].width = float(10)
sheet.column_dimensions['G'].width = float(50)
sheet.column_dimensions['H'].width = float(15)
sheet.column_dimensions['I'].width = float(32)
sheet.column_dimensions['J'].width = float(25)
sheet.column_dimensions['K'].width = float(50)
red_background = PatternFill(bgColor=colors.RED)
diff_style = DifferentialStyle(fill=red_background)
rule = Rule(type="expression", dxf=diff_style)
rule.formula = ["NOT(ISBLANK($J1))"]
sheet.conditional_formatting.add("A1:Y100", rule)
for row in dataframe_to_rows(teams, index=False, header=True):
sheet.append(row)
for rows in sheet.iter_rows(min_row=1, max_row=None, min_col=None, max_col=None):
for cell in rows:
cell.alignment = Alignment(vertical='center', wrapText=True)
workbook.save(output_file_name)
首先,不要循環導入,否則您將度過糟糕的一天。 在代碼頂部導入 go,請參閱PEP8 指南以獲取更多信息以及其他關於樣式 Python 代碼的指南。
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.styles import Alignment, PatternFill, colors
from openpyxl.formatting.rule import FormulaRule
split_values = submitted_and_revised['Client Name'].unique()
print(split_values)
workbook = Workbook()
sheet = workbook.active
for value in split_values:
teams = submitted_and_revised[submitted_and_revised['Client Name'] == value]
output_file_name = "Team_" + str(value) + ".xlsx"
其次,您不必自己創建規則,因為您使用的是簡單的FormulaRule 。 現在您只需要將規則應用於行中的所有單元格:
for row in dataframe_to_rows(teams, index=False, header=True):
sheet.append(row)
for rows in sheet.iter_rows(min_row=1, max_row=None, min_col=None, max_col=None):
for cell in rows:
cell.alignment = Alignment(vertical='center', wrapText=True)
sheet.conditional_formatting.add(f'{cell.coordinate}',
FormulaRule(formula=["=NOT(ISBLANK(J{cell.row}))"],
fill=red_background))
最后它應該看起來像這樣,當單元格J{row}
等於working_revised['New Record ID']
時,該特定行將具有紅色背景。 請注意,如果這不是您的意圖,您正在為循環的每個迭代創建一個新的工作簿:
workbook = Workbook()
sheet = workbook.active
跳出循環。
最終代碼:
from openpyxl import Workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from openpyxl.styles import Alignment, PatternFill, colors
from openpyxl.formatting.rule import FormulaRule
split_values = submitted_and_revised['Client Name'].unique()
print(split_values)
red_background = PatternFill(bgColor=colors.RED)
for value in split_values:
teams = submitted_and_revised[submitted_and_revised['Client Name'] == value]
output_file_name = "Team_" + str(value) + ".xlsx"
workbook = Workbook()
sheet = workbook.active
#Format Column Widths
#sheet.column_dimensions['C'].auto_size = True
sheet.column_dimensions['B'].width = float(18)
sheet.column_dimensions['C'].width = float(5)
sheet.column_dimensions['C'].width = float(25.25)
sheet.column_dimensions['D'].width = float(20)
sheet.column_dimensions['E'].width = float(6)
sheet.column_dimensions['G'].width = float(65)
sheet.column_dimensions['H'].width = float(20)
sheet.column_dimensions['I'].width = float(14)
sheet.column_dimensions['J'].width = float(14)
sheet.column_dimensions['K'].width = float(50)
for row in dataframe_to_rows(teams, index=False, header=True):
sheet.append(row)
for rows in sheet.iter_rows(min_row=2, max_row=None, min_col=None, max_col=None):
for cell in rows:
cell.alignment = Alignment(vertical='center', wrapText=True)
sheet.conditional_formatting.add(f'{cell.coordinate}',
FormulaRule(formula=["=NOT(ISBLANK(J{cell.row}))"],
fill=red_background))
workbook.save(output_file_name)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.