I wasn't able to figure out what to search for this, so if it's already out there, I apologize in advance, and would be happy to just take a search reference to somewhere else.
Some background: I'm working on some extra features for a python Excel library and one thing I'd like to be able to do is apply a style to a range of cells in a single call. Right now, our code permits us to do something like ws[1][1].font.bold = True to bold cell A1. We also have a range operator that permits us to do something like ws.range("A1:B2").value = 1 to set all the values in that range to 1.
What I'd like to do now is add the ability to do ws.range("A1:B2").font.bold = True, but I can't figure out how. range("A1:B2") is an object that doesn't explicitly reference the cells (just the two corners to save on space), but does not explicitly have the .font or .value attribute, rather it's intercepted via a @property decorator to apply appropriately.
The thing is, if I add an @property decorator to create a font property, I can return a new font object, but I have no way of getting this font object after the bold attribute has been modified to apply it to all the cells in the range.
I was thinking I could yield the font, then add the apply logic after the yield, but that doesn't work as yield returns a generator. The only other idea I can think of is to create an explicit .font object, then in the Range class, override .font.__setattr__ to listen for changes, but then I can't figure out how to get the original range from within __setattr__ that needs to be applied without having to create a third intermediary class. Not only does that seem rather awkward, it also seems incredibly unpythonic.
Is something like this possible without having to result to rather esoteric methods? I hope my explanation was clear and again, if it's already out there, feel free to point me to an existing reference -- I just didn't know what to search for.
.fontobject that holds a reference to the originalRange. Then override__setattr__()in the.fontto update styles of all cells in the range.Rangereturn the same "fake" property for any attribute name, and have it delegate all__setattr__calls to the original.font's attributes to be applied to all cells in a range. So, you intercept writes tofont's attributes, and apply them to all cells in the correct range. It's really the most straightforward way to do it - you don't need to detect modifications on the.fontor anything, you just write back things immediately.