1

I'm trying to add some icon sets to an existing excel file using python. The excel file is written using xlsxwriter. As xlsxwriter does not support icon sets, I close the file, reopen it with openpyxl, add the icon sets and save it again. Problem is, that I loose all conditional formatting added previously. Opening the file in openpyxl with "keep_vba=True" results in a non-readable xlsx-File.

Any ideas how to achieve this?

Thanks in advance!

P.S.: Missed some details. Sorry for that. I write xlsx files in both cases (xlsxwriter and openpyxl) and use python 2.7 and the latest versions of openpyxl and xlsxwriter on a windows machine with excel 2013. Icon sets are little symbols like arrows (up, down) which can be used in conditional formatting.

2
  • So, you converted your xls file to xlsx, right? What do you call "icon sets", can you give us an example? Commented Dec 20, 2016 at 10:31
  • I only work with xlsx files. No xls involved. Icon sets are e.g. the traffic lights you can add to a cell: link Commented Dec 20, 2016 at 11:16

1 Answer 1

1

OpenPyXl has a support for conditional formatting and Icon Sets.

See the official documentation: Conditional Formatting > IconSet

Here is an example:

>>> from openpyxl.formatting.rule import IconSet, FormatObject
>>> first = FormatObject(type='percent', val=0)
>>> second = FormatObject(type='percent', val=33)
>>> third = FormatObject(type='percent', val=67)
>>> iconset = IconSet(iconSet='3TrafficLights1', cfvo=[first, second, third], showValue=None, percent=None, reverse=None)
>>> # assign the icon set to a rule
>>> from openpyxl.formatting.rule import Rule
>>> rule = Rule(type='iconSet', iconSet=iconset)
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I think I didn't describe the problem well: The conditional formatting added with openpyxl works just fine and I see the icon sets. But all other conditional formats which were in the original file are lost.
It sounds like a bug in OpenPyXl :-(
I've been afraid of that. Thanks for your help and time!
Why should it be a bug in openpyxl? Could be something else. Anyway. I wouldn't try and mix the two libraries like this. Probably makes more sense to create the file in openpyxl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.