Am .04.2016, 18:02 Uhr, schrieb Adam Morris <
w...@myemptybucket.com>:
> Hi, with the 2.4 alpha (or possibly earlier, I'm still stuck on a 2.1
> variant), is using Style() depreciated?
I think you mean "deprecated"? Though who knows, there may well special
accounting rules for software components.! ;-)
Anyway, you should definitely upgrade to 2.3: it's much nicer and faster.
A single, immutable Style() object for cells is indeed deprecated.
> Is there a preferred way to copy cell styles?
Well, it's best not thinking about them as styles.
> Where before I would do something like;
>
> desc_cell.style = src_cell.style.copy(number_format='XXX')
>
>
> Should I be doing something like:
>
> def copy_style(src_cell, dest_cell):
> dest_cell.font = src_cell.font
> dest_cell.fill = src_cell.fill
> dest_cell.border = src_cell.border
> dest_cell.alignment = src_cell.alignment
> dest_cell.number_format = src_cell.number_format
>
> copy_style(src_cell, dest_cell)
> dest_cell.number_format = 'XXX'
That would work though it's faster to copy the underlying StyleArray:
c1 = ws['A1']
c2 = ws['B2']
c2._style = copy(c1._style)
However, what I am working on are NamedStyles. These represent styles that
are supposed to be shared just like the "Style Templates" in Excel. The
API will be something like this:
normal = NamedStyle(name="Normal", font=Font(…), etc.)
wb.named_styles.add(normal)
ws['A1'].style = "Normal"
This would hopefully be easy to work with and also very fast.
However, there are a few implementation hurdles to jump:
* named styles and cell formats are, according to the specification,
supposed to be commutative so that you could define a named style, apply
it to range of cells, change it, add local cell formats and everything
would look great. Except Excel explicitly doesn't follow the standard
here. Also it's not clear how to resolve possible conflicts. What do you
do when a cell font is bold but the named style has font.bold = False?.
This is particularly important if the style is supposed to mutable after
assignment.
The simplest implementation would do sort of what Excel does and copy the
attributes of the named style as well as maintaining a link to it but this
makes a lot of assumptions about use in code: a named style would need to
be immutable after being assigned once; existing cell formats would have
to be replaced en masse but could then be overwritten. Or resolution has
to be done when serialising the cell, which could slow things down a lot.
Also the code for the API is a bit tricky.
Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel:
+49-211-600-3657
Mobile:
+49-178-782-6226