openpyxl: How to get cell background colour property?

This is the place for queries that don't fit in any of the other categories.

openpyxl: How to get cell background colour property?

Postby giridhar276 » Sun Aug 03, 2014 12:47 am

I have few lines of text in spreadsheet(*.xlsx format) and able to read the info using openpyxl library in python.

Now some cells in spreadsheet has background color and want to get the background color too.

My actual plan is read the cell info along with background color and then display the cell data along with background color in html web page .

I am able to display the info but could not able to get the background color property.

Could you please help me in this regard?

Code: Select all
import openpyxl
wb = openpyxl.load_workbook(filename = r'empty.xlsx')
sheet_ranges = wb['range']
print sheet_ranges['D18'].value


Regards,
Giridhar
Last edited by Yoriz on Sun Aug 03, 2014 1:57 am, edited 1 time in total.
Reason: First post lock, Changed tittle.
giridhar276
 
Posts: 1
Joined: Sun Aug 03, 2014 12:41 am

Re: openpyxl: How to get cell background colour property?

Postby Yoriz » Sun Aug 03, 2014 1:58 am

Welcome to the forum, Please read the new user read this link in my signature.

Google told me about the following link on stackoverflow:
how to read the xlsx color infomation by using openpyxl
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1158
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: openpyxl: How to get cell background colour property?

Postby Larz60+ » Sun Aug 03, 2014 12:57 pm

Hello,

There is a page in the documentation http://openpyxl.readthedocs.org/en/2.0.2/styles.html?highlight=attributes that shows how to change style attributes. It has the names of the color attributes, I haven't used this module, but it seems as though the following structure has what you need to fetch and/or adjust color. The entire style has to be reed or written at the same time.

Code: Select all
Style(font=Font(name='Calibri',
                size=11,
                bold=False,
                italic=False,
                superscript=False,
                subscript=False,
                underline='none',
                strikethrough=False,
                color=Color(index='FF000000')),
      fill=Fill(fill_type=None,
                rotation=0,
                start_color=Color(index='FFFFFFFF'),
                end_color=Color(index='FF000000')),
      borders=Borders(left=Border(border_style='none',
                                  color=Color(index='FF000000')),
                      right=Border(border_style='none',
                                   color=Color(index='FF000000')),
                      top=Border(border_style='none',
                                 color=Color(index='FF000000')),
                      bottom=Border(border_style='none',
                                    color=Color(index='FF000000')),
                      diagonal=Border(border_style='none',
                                      color=Color(index='FF000000')),
                      diagonal_direction=0,
                      all_borders=Border(border_style='none',
                                         color=Color(index='FF000000')),
                      outline=Border(border_style='none',
                                     color=Color(index='FF000000')),
                      inside=Border(border_style='none',
                                    color=Color(index='FF000000')),
                      vertical=Border(border_style='none',
                                      color=Color(index='FF000000')),
                      horizontal=Border(border_style='none',
                                        color=Color(index='FF000000'))),
      alignment=Alignment(horizontal='general',
                          vertical='bottom',
                          text_rotation=0,
                          wrap_text=False,
                          shrink_to_fit=False,
                          indent=0),
      number_format=NumberFormat(format_code='General'),
      protection=Protection(locked='inherit',
                            hidden='inherit'))


Since I haven't used it, this is about all I can offer,

Have fun
Larz60+
Larz60+
 
Posts: 289
Joined: Thu Apr 03, 2014 4:06 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests