I have never used if or else statements in python, and I can’t solve this easy problem:
from openpyxl import Workbook, load_workbook
from openpyxl.styles import PatternFill
wb = load_workbook("variables.xlsx")
ws = wb.active
if "A1" != "nameVariable":
ws.append(["nameVariable", "value"])
ws["A1"].fill = PatternFill("solid", start_color="FFA500")
ws["B1"].fill = PatternFill("solid", start_color="FFA500")
It just fills the cell, but I don’t want it, bacause then this happens:
enter image description here
Also, does anybody knows why it doesn’t print in row 2?
Thanks in advice!
>Solution :
You are comparing the string "A1" and the string "nameVariable". To compare the content of the cell, use:
if ws["A1"].value != "nameVariable":