Closed barnacle-carnival closed 1 year ago
The objects returned by the model are typically Python representations of Google protobufs and you can just traverse them with dot parameters, so the code becomes:
def cell_bg_color(cell: object) -> Union[Tuple, List[Tuple]]:
if cell._storage is None or cell._storage.cell_style_id is None:
return None
cell_style = cell._model.table_style(cell._table_id, cell._storage.cell_style_id)
cell_properties = cell_style.cell_properties.cell_fill
if cell_properties.HasField("color"):
return (
int(cell_properties.color.r * 65536),
int(cell_properties.color.g * 65536),
int(cell_properties.color.b * 65536),
)
elif cell_properties.HasField("gradient"):
return [
(int(s.color.r * 65536), int(s.color.g * 65536), int(s.color.b * 65536))
for s in cell_properties.gradient.stops
]
At least for a couple of variants of colours.
And guessing on the colour depth is 16-but but might 8-bit.
So there's a new bg_color
property for cells that should return None
if there isn't one. Is present on all cell types. I'll add to the docs at some point soon and publish to PyPI. Right now it's just on main
.
And now published as 3.11.0. See README for usage.
I've figured out a way to extract the cell background color by digging deep into a cells and extracting the style data. I cribbed heavily from the background image code to figure out how to get this data. It would be nice if this had a dedicated API. I wrote a very lame hack function as a proof of concept, it gets the job done... I could parse the style data, but I that's probably better done via the API. I don't need the API to guess color names, I can do that myself, I just shouldn't have to parse the string or use such a convoluted method to get it.
Here's my lame function: