I've created a dataset using a Pandas Dataframe where I have two columns, one of which has various hex colour codes that have been automatically generated from another dataset. I want to check that the hex colour input is what is expectedso it would be helpful to be able to fill colour each of the cells with the hex code provided. I have several codes to check through, so would be helpful to have this automatically done in Python prior to exporting to excel.
Cells filled by hex colour in cell

Any help would be greatly appreciated.
I've looked at this solution, but it seems too generic for what I'm after Coloring Cells in Pandas
And this solution is closest to what I'm after but applymap seems to have depreciated so I can't get it to work Python - color DF cell by hex value in cell
There doesn't seem to be an API for direct conversion from Pandas styling to Excel styling. As a result, I would suggest applying the color formatting AFTER the dataframe is converted to an Excel object. This could be done in-memory with a Pythonic Excel API - for which I chose
openpyxlin the following sample code, but it could also bexlsxwriteror anything capable.Edit: It is also possible to convert DataFrame rows to Worksheet rows via
dataframe_to_rows()inopenpyxl. The code might be cleaner, but a slow row-iteration loop is still present.Solution
Result
test.xlsxopened with LibreOfficeTest environment
Python(fromconda):3.12.2Pandas:2.1.4Openpyxl:3.0.10OS: Debian 12 (x86-64)