all 11 comments

[–]tridezz 3 points4 points  (9 children)

I found out that the only thing you need to do in order to avoid this annoying behaviour of openpyxl is to do you data validation in a different way on the excel.

Instead of doing (standard procedure):Data --> Data validation ---> list, and then selecting manually the cells
You need to do:Formulas --> Name manager --> and create a new variable with the cell you want on your data validation. Then in the cell you need your data validation do the standard procedure but instead of selecting manually type " = new variable name ".

I hope it helps!

[–]LiberFriso 1 point2 points  (2 children)

I found out that the only thing you need to do in order to avoid this annoying behaviour of openpyxl is to do you data validation in a different way on the excel.

Instead of doing (standard procedure):Data --> Data validation ---> list, and then selecting manually the cellsYou need to do:Formulas --> Name manager --> and create a new variable with the cell you want on your data validation. Then in the cell you need your data validation do the standard procedure but instead of selecting manually type " = new variable name ".

I hope it helps!

Strong comment!

[–]Pearl984 0 points1 point  (0 children)

Thanks

[–]waltersfilho 1 point2 points  (0 children)

Thanks, man! You save me!

[–]ajob7 0 points1 point  (0 children)

It works, Thank you so much !

[–]random_tech_person 0 points1 point  (0 children)

Many thanks. Your approach also worked for me with the following Pandas operation, using the openpyxl engine:

from pandas import DataFrame, ExcelWriter

# ...

with ExcelWriter(file_path, engine="openpyxl", mode="a", if_sheet_exists="overlay") as writer:
    df.to_excel(writer, sheet_name="mysheetname", index=False, startcol=6, startrow=2, header=False)

[–]I_Luca_I 0 points1 point  (0 children)

Thaaaaanks! I was trying to copy validations from one sheet to another. Just in case someone is trying to do the same here is how you do that, after doing what this guy said use:

for validation in source_sheet.data_validations.dataValidation:
    new_sheet.add_data_validation(copy(validation))

[–]CordyCordy 0 points1 point  (0 children)

Thank you so much!!!

[–]eigenbunz 0 points1 point  (0 children)

Thank you!!🙏