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 6323637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:33:23+00:00 2026-05-24T16:33:23+00:00

I am creating a table using the python module reportlab. In this table, I

  • 0

I am creating a table using the python module reportlab. In this table, I would like to loop through and have a different background color depending on the values of any particular cell.

To do this, I came up with the following:

elements = []

table1 = [[34,27,35,35],
          [3,76,23,157],
          [13,137,15,75],
          [56,26,46,26]]




t1 = Table(table1)
for ii in range(len(table1)):
    for jj in range(len(table1)):
        if table1[ii][jj] <=50:
            ourcolor = colors.white
        elif table1[ii][jj] <=100:
            ourcolor = colors.skyblue
        elif table1[ii][jj] <=200:
            ourcolor = colors.green

        else:
            ourcolor = colors.white
        t1.setStyle(TableStyle([('BACKGROUND', (ii,jj), (ii,jj), ourcolor),
                                ('ALIGN', (0,0), (-1,-1), 'CENTER'),
                                ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                                ('BOX', (0,0), (-1,-1), 0.25, colors.black)
                                ]))

elements.append(t1)

But, many of the cells still are not colored and many of them are colored incorrectly, however some of them are correct. I am assuming something is wrong with my loop since I am not a very experienced programmer.

Any help or ideas would be greatly appreciated.

  • 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-24T16:33:24+00:00Added an answer on May 24, 2026 at 4:33 pm

    I don’t know enough about ReportLab to know for sure, but a common problem in this type of coding is that the axes are swapped. For example, indexing like this: table1[ii][jj] means that ii is the y axis (rows) and jj is the x axis (columns), so you’d have to supply x and y to ReportLab as jj, ii. Check if your output has rows and columns swapped when coloring cells.

    Also, note that your double loop is looping over the same range twice, which works only because your table is square. If your table even becomes non-square, you’ll have the wrong range on one of your loops.

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

Sidebar

Related Questions

I am creating a large table dynamically using Javascript. I have realised the time
I'm creating a table that looks something like this. CREATE TABLE packages ( productCode
I am going through a problem that i am creating a html table using
I am dynamically creating an HTML table by using a repeater. I have four
I have a table created using Python + Sqlite3 c.execute('CREATE TABLE IF NOT EXISTS
I'm attempting at creating a menu from a table using the Suckerfish css menu
Using MS Access 2007, I am creating a student management database. I have tables
I am dynamically creating a table which I want to have clickable rows. When
I have a temp table I am creating a query off of in the
I have new to python and django I am creating poll application and in

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.