Applying Styles - Documentation Issue or a bug?

76 views
Skip to first unread message

Rakesh K

unread,
Apr 19, 2015, 1:03:25 PM4/19/15
to openpyx...@googlegroups.com

First, thanks a lot for this wonderful library.

I'm trying to apply a style to an entire row and noticed that the documentation is not right here.
https://openpyxl.readthedocs.org/en/latest/styles.html

>>> from openpyxl.workbook import Workbook
>>> from openpyxl.styles import Font, Fill
>>> wb = Workbook()
>>> ws = wb.active
>>> c = ws['A1']
>>> row = ws.row_dimensions[1]
>>> row.style = Font(underline="single")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/rakesh.varma/Envs/sm_reports/lib/python2.7/site-packages/openpyxl/styles/styleable.py", line 124, in style
    self.font = value.font.copy()
AttributeError: 'Font' object has no attribute 'font'
>>>

Font cannot be directly applied to the style. Instead, Style object needs to created with a font in it.

Eg:
from openpyxl.styles import Style
row.style = Style(Font(underline="single"))


Please let me know if this is a documentation issue or a bug. I've tested it on this version.
openpyxl==2.2.1

Thanks,
Rakesh

Charlie Clark

unread,
Apr 19, 2015, 1:14:00 PM4/19/15
to openpyx...@googlegroups.com
Hiya,

Am .04.2015, 19:03 Uhr, schrieb Rakesh K <varma....@gmail.com>:

> First, thanks a lot for this wonderful library.
> I'm trying to apply a style to an entire row and noticed that the
> documentation is not right here.
> https://openpyxl.readthedocs.org/en/latest/styles.html
>
>>>> from openpyxl.workbook import Workbook
>>>> from openpyxl.styles import Font, Fill
>>>> wb = Workbook()
>>>> ws = wb.active
>>>> c = ws['A1']
>>>> row = ws.row_dimensions[1]
>>>> row.style = Font(underline="single")

This is an error in the documentation. It should be just:
row.font = Font(underline="single")

However, it's worth nothing that this will not do what you want and format
a whole row. According to the specification styles for rows and columns
are used by Excel to apply the format to new cells when they are created
(but not to existing cells). openpyxl just sets the information on the
columns and rows but doesn't make use of it itself.

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

Rakesh K

unread,
Apr 19, 2015, 7:07:45 PM4/19/15
to openpyx...@googlegroups.com
Thanks Charlie.
Reply all
Reply to author
Forward
0 new messages