How can I create a table with reportlab using objects in an existing list

StackOverflow https://stackoverflow.com/questions/22640573

  •  21-06-2023
  •  | 
  •  

سؤال

At the moment I have a list of objects as below. And I can print them out by iterating over them no problem. But I don't understand how I can print these out in a table.

people = [("John","Smith"), ("Jane","Doe"), ("Jane","Smith")]

for x in people:

    person = x

    lineText = (person.getFirstName() + " " + person.getLastName())

    p = Paragraph(lineText, helveticaUltraLight)
    Story.append(p)

I had a look at this example. Specifically the enumeration of the users in the example. However this always falls over.

هل كانت مفيدة؟

المحلول

I figured it out:

people = [("John","Smith"), ("Jane","Doe"), ("Jane","Smith")]
table_data = []
for i, person in enumerate(people):
    # Add a row to the table
    table_data.append([person[0], person[1]])
# Create the table
issue_table = Table(table_data, colWidths=[doc.width/3.0]*3)

Story.append(issue_table)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top