Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If statement always run in Python with openpyxl, even if it's false

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are comparing the string "A1" and the string "nameVariable". To compare the content of the cell, use:

if ws["A1"].value != "nameVariable":
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading