Setting alignment for multiple cells in a row

4,222 views
Skip to first unread message

Bill St. Louis

unread,
Jan 28, 2016, 3:50:26 PM1/28/16
to openpyxl-users
Please bear with me, as I'm new with both Python and openpyxl...using 2.4

I'm trying to set cell alignment for the first row in a sheet (header).  It works fine for a single cell, but if I do this:

header = ws['A1':'M1']
al
= Alignment(horizontal='center', vertical='center')
header
.alignment = al

I get "AttributeError: 'generator' object has no attribute 'alignment'"

I know I'm doing something amazingly basic wrong, but I don't know what.  Help?

thanks!
bill

Charlie Clark

unread,
Jan 28, 2016, 4:00:08 PM1/28/16
to openpyx...@googlegroups.com
Hi Bill,

Am .01.2016, 21:50 Uhr, schrieb Bill St. Louis <wfs...@gmail.com>:

> Please bear with me, as I'm new with both Python and openpyxl...using 2.4

I do hope you are not using 2.4 as it's still in development and currently
broken!

> I'm trying to set cell alignment for the first row in a sheet (header).
> It
> works fine for a single cell, but if I do this:
>
> header = ws['A1':'M1']
> al = Alignment(horizontal='center', vertical='center')
> header.alignment = al
>
> I get "AttributeError: 'generator' object has no attribute 'alignment'"

> I know I'm doing something amazingly basic wrong, but I don't know what.
> Help?

ws['A1':'M1'] returns a range of cells that you need to work with:

for cell in ws['A1':'M1']:
cell.alignment = al

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

Bill St. Louis

unread,
Jan 28, 2016, 5:04:09 PM1/28/16
to openpyxl-users
Thank you so much, Charlie, for the quick reply.  I'll have to take a look at the code I installed.  I THOUGHT it was 2.4...I used pip to install it, but I probably just chose the default...which based on what you're saying is probably NOT 2.4.  :-)

Bill St. Louis

unread,
Jan 28, 2016, 5:07:19 PM1/28/16
to openpyxl-users
and, after checking, it is 2.3.3.  Whew!


On Thursday, January 28, 2016 at 3:00:08 PM UTC-6, Charlie Clark wrote:

Bill St. Louis

unread,
Jan 28, 2016, 7:15:06 PM1/28/16
to openpyxl-users
My apologies for continuing to bug you with what are probably trivial questions I should know the answer to, but let me try one more time.  When I put that code in, this is what pops up:


>>> for cell in ws['A1':'M1']:
    cell.alignment = al

   
Traceback (most recent call last):
  File "<pyshell#20>", line 2, in <module>
    cell.alignment = al
AttributeError: 'tuple' object has no attribute 'alignment'

I can print the cell objects as it iterates through A1 thru M1 (looks like this: "<openpyxl.cell.read_only.ReadOnlyCell object at 0x1073c1a98>, <openpyxl.cell.read_only.ReadOnlyCell object at 0x1073c1af0>"), but when I try to access the attributes, it's not allowing it.  I can't print any of the object attributes or change them.

If this is simply a matter of my utter ignorance in Python, feel free to redirect me to the books :-)  I have been writing code for almost 30 years, but not in this particular language.

Thank you again.


On Thursday, January 28, 2016 at 3:00:08 PM UTC-6, Charlie Clark wrote:

Charlie Clark

unread,
Jan 29, 2016, 3:07:19 AM1/29/16
to openpyx...@googlegroups.com
Am .01.2016, 01:15 Uhr, schrieb Bill St. Louis <wfs...@gmail.com>:

> My apologies for continuing to bug you with what are probably trivial
> questions I should know the answer to, but let me try one more time.
> When
> I put that code in, this is what pops up:
>
>>>> for cell in ws['A1':'M1']:
> cell.alignment = al
>
> Traceback (most recent call last):
> File "<pyshell#20>", line 2, in <module>
> cell.alignment = al
> AttributeError: 'tuple' object has no attribute 'alignment'

Sorry, my mistake. The range is always a collection of rows.

for row in ws['A1':'M1']:
for cell in row:
cell.alignment = al

> I can print the cell objects as it iterates through A1 thru M1 (looks
> like
> this: "<openpyxl.cell.read_only.ReadOnlyCell object at 0x1073c1a98>,
> <openpyxl.cell.read_only.ReadOnlyCell object at 0x1073c1af0>"), but when
> I
> try to access the attributes, it's not allowing it. I can't print any of
> the object attributes or change them.

If you're using read-only mode you won't be able to change anything.
Stands to reason.

Bill St. Louis

unread,
Jan 29, 2016, 4:19:13 PM1/29/16
to openpyxl-users
Yeah, I saw that read-only thing.  I just poked around a bit, and I now see that using "use_iterators=true" on the load_workbook call sets the sheet to read-only.  I had been trying different ways to run through the columns and that was a byproduct.

Lesson learned.

I will experiment more this evening.  Thanks much!
Reply all
Reply to author
Forward
0 new messages