Copy cell style

2,178 views
Skip to first unread message

Ben Collier

unread,
Mar 26, 2014, 6:58:19 AM3/26/14
to openpyx...@googlegroups.com
If I do:

ws.cell(row = 5 + n, column = 68).style = ws.cell(row = 5, column = 68).style

I get an attribute error.

Is it possible to copy styles between cells?

Thanks,

Ben

Charlie Clark

unread,
Mar 26, 2014, 7:07:04 AM3/26/14
to openpyx...@googlegroups.com
Hi Ben.
Not directly, no. It can be done indirectly: ws._styles['A1'] =
copy(ws._styles['A2']) but you are probably better off assigning a style
to a range of cells.

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

Ben Collier

unread,
Mar 26, 2014, 7:12:18 AM3/26/14
to openpyx...@googlegroups.com
Hi, thanks Charlie, I'm getting "global name 'copy' is not defined" when I try that?


--
You received this message because you are subscribed to a topic in the Google Groups "openpyxl-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openpyxl-users/BwuFKoV7syw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openpyxl-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Collier

unread,
Mar 26, 2014, 7:13:53 AM3/26/14
to openpyx...@googlegroups.com
Sorry! Now imported copy. Duh.

Charlie Clark

unread,
Mar 26, 2014, 7:13:57 AM3/26/14
to openpyx...@googlegroups.com
Am .03.2014, 12:12 Uhr, schrieb Ben Collier <bmco...@gmail.com>:

> Hi, thanks Charlie, I'm getting "global name 'copy' is not defined" when
> I
> try that?

Sorry, should have put in "something like…"

from copy import copy, deepcopy

Styles in openpyxl are a bit of a mess (much of this has to do with the
mess they are in Excel itself) so caution is required. Fortunately, Eric
is working on significant improvements.

Ben Collier

unread,
Mar 26, 2014, 7:14:53 AM3/26/14
to openpyx...@googlegroups.com
Yep, sorry - just realised as I sent my message. Slight numbskull moment here.


张少华

unread,
Apr 15, 2014, 8:34:39 PM4/15/14
to openpyx...@googlegroups.com
hi charlie,
   I have some question, I try it, but it throws exception
Traceback (most recent call last):
  File "./zhoubao.py", line 82, in <module>
    ws._styles[add] = copy(rsh._styles[add])
KeyError: 'A1'


在 2014年3月26日星期三UTC+8下午7时07分04秒,Charlie Clark写道:

Charlie Clark

unread,
Apr 17, 2014, 12:10:41 PM4/17/14
to openpyx...@googlegroups.com
Am .04.2014, 02:34 Uhr, schrieb 张少华 <zhangshaoh...@gmail.com>:

> hi charlie,
> I have some question, I try it, but it throws exception
> Traceback (most recent call last):
> File "./zhoubao.py", line 82, in <module>
> ws._styles[add] = copy(rsh._styles[add])
> KeyError: 'A1'

I'd need to see the whole code and a sample file to see what's wrong.

parth...@gmail.com

unread,
Mar 22, 2017, 1:07:50 PM3/22/17
to openpyxl-users
Hi Charlie,
I am trying to use  ws._styles['A1'] =  copy(ws._styles['A2']) but I get the below error:

AttributeError: 'Worksheet' object has no attribute '_styles'

Statement used in my code:
ws1._styles['E2'] = copy(ws1._styles['D2'])


The style that I want to copy is:
I have a cell with date in format 'DD-MM-YYYY'. 
Using Excel's Custom Format Cell option, this cell displays only 'DD-MMM' whereas the formula bar show entire 'DD-MM-YYYY'.
I want to copy this same style to a new cell where I will be adding the current date.

If there is some other way to achieve this then it would be a great help.
Thanks in advance.

Parth Karia
Reply all
Reply to author
Forward
0 new messages