I have a problem with formating blank cells. My code is
import setting_prices
import pandas as pd
from openpyxl import Workbook, load_workbook
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles.differential import DifferentialStyle
from openpyxl.formatting.rule import ColorScaleRule, CellIsRule, FormulaRule
import os
from datetime import datetime
def load_file_apply_format():
wb = load_workbook(filename)
writer = pd.ExcelWriter(filename, engine='openpyxl')
writer.book = wb
prices.to_excel(writer, sheet_name=today_as_str)
ws = wb[today_as_str]
redFill = PatternFill(start_color='EE1111',
end_color='EE1111',
fill_type='solid')
# whiteFill = PatternFill(start_color='FFFFFF',
# end_color='FFFFFF',
# fill_type='solid')
ws.conditional_formatting.add('B2:H99',
CellIsRule(operator='lessThan',
formula=['$I2'],
stopIfTrue=False, fill=redFill))
writer.save()
prices = setting_prices.df
today_as_str = datetime.strftime(datetime.now(), ' %d_%m_%y')
desktop_path = os.path.expanduser("~/Desktop")
filename = 'price_check.xlsx'
if os.path.exists(filename):
load_file_apply_format()
else:
prices.to_excel(filename, sheet_name=today_as_str)
load_file_apply_format()
My formula works just fine, but excel treats blanks cells as 0 therefore they are always less than column I, and formats them. I would like to skip blank cells, or to format them to looks like regular cells.
I have tried almost all suggestions from the forum but seems like that I'm not able to fix it.
Please give me some suggestions.
@Greg answer for using :
ws.conditional_formatting.add('B2:H99',
CellIsRule(operator='between',
formula=['1', '$I2'],
stopIfTrue=False, fill=redFill))
results in formating cells which are == to 'I' column which I want to avoid. Also 'between' is setting format to all blank cells if cell in 'I' column is empty.
For example:
All cells for productA must be default formated because they are equal to cell in column 'I'.
For productB the only formatted cell must be G3 because is lower then I3.
All cells for productC must be default formatted because cell in I is empty.
I thought that if I use my formatting code for lessThen, and another formula for blanks cells would do the work. But I was not able to make it work.
