Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7589607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:12:13+00:00 2026-05-30T20:12:13+00:00

I’m trying to apply a style that will highlight the whole row if one

  • 0

I’m trying to apply a style that will highlight the whole row if one of the columns contains the value “Assets”. The code below will highlight only the column with “Assets” in it, instead of the entire row. Is there a way to apply the style to the whole row?

for row in csv_input:
     #Iterate through each column
      for col in range(len(row)):
           #Apply different styles depending on row
           if row_count == 0:
               sheet.write(row_count,col,row[col],headerStyle)
           elif row_count == 3:
               sheet.write(row_count,col,row[col],subheadStyle)
           elif "Assets" in row[col]:
               sheet.write(row_count,col,row[col],highlightStyle)             
           else:
               if (is_number(row[col]) == True):
                   sheet.write(row_count,col,float(row[col]),rowStyle)
               else:
                   sheet.write(row_count,col,row[col],rowStyle)

As you can see, depending on the row I am applying different styles. How can I make it so that any row that contains the keyword “Assets” will be highlighted? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:12:14+00:00Added an answer on May 30, 2026 at 8:12 pm

    Your main problem is that your code is checking for “Assets” after it has written some cells in the row. You need to do your “what style to use for the whole row” tests before you write any cells in the row. Setting a style on the xlwt Row object doesn’t work; that’s a default style for use with cells that don’t have any formatting applied otherwise.

    Other problems:

    contains the value “Assets”. The code below will highlight only the
    column with “Assets” in it

    This is ambiguous. Suppose a cell value is exactly equal to “Equity Assets”; what do you want to do? Note: your code will highlight such a cell and those to its right. Also it’s not apparent whether the “Assets”-bearing cell should be the first (example in your comment on another answer) or any cell (as per your code).

    Some of your choices for variable names make your code very hard to read e.g. row is a list of cell values but col is a column index. Use enumerate() where possible.

    Try something like this:

    for row_index, cell_values in enumerate(csv_input):
        # Determine what style to use for the whole row
        if row_index == 0:
            common_style = headerStyle
        elif row_index == 3:
            common_style = subheadStyle
        elif "Assets" in cell_values:
            # perhaps elif any("Assets" in cell_value for cell_value in cell_values):
            # perhaps elif cell_values and cell_values[0] == "Assets":
            # perhaps elif cell_values and "Assets" in cell_values[0]:
            common_style = highlightStyle
        else:
            common_style = rowStyle
        # Iterate over the columns
        for col_index, cell_value in enumerate(cell_values):
            if common_style == rowStyle and is_number(cell_value):
                cell_value = float(cell_value)
            sheet.write(row_index, col_index, cell_value, common_style)
    

    I’m curious about the is_number function … I’d use this:

    def is_number(s):
        try:
            float(s)
            return True
        except ValueError:
            return False
    

    which automatically leads to:

            if common_style == rowStyle:
                try:
                    cell_value = float(cell_value)
                except ValueError:
                    pass
    

    and also raises the question of whether you should perhaps have different styles for numbers and text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.