ReportLab - error when creating a table

2024/10/12 13:25:23

This is the first time I've used ReportLab, I have tried to edit an existing script that does exactly what I want to do, but I get the following error, when I try and run the script.

Script -

import os, arcgisscripting, datetime, getpass, stringfrom reportlab.lib.pagesizes import A4
from reportlab.platypus import *
from reportlab.lib import colors
from reportlab.lib.styles import ParagraphStylemyWorkspace = r"W:\City.gdb"
myReportFolder = r"W:\Reports"
myLogosFolder = r"W:\Logos"
OpenPDF = "true"gp = arcgisscripting.create(9.3)#setup geoprocessor
gp.workspace = myWorkspace
gp.toolbox = "analysis"
gp.OverwriteOutput = 1
mySelectedGroupsFC = myWorkspace + os.sep + "SelectedGroups"
myNewBusinessFC = myWorkspace + os.sep + "New_Businesses"
myBufferFC = myWorkspace + os.sep + "Buffer"
myReportTable = myWorkspace + os.sep + "FINAL_TABLE"#obtain Selected groups
mySelGroupsCursor = gp.searchcursor(mySelectedGroupsFC)
mySelectedGroups = mySelGroupsCursor.next()#obtain New Business groups
myNewBusinessCursor = gp.searchcursor(myNewBusinessFC)
myNewBusiness = myNewBusinessCursor.next()#obtain Buffer
myBufferCursor = gp.searchcursor(myBufferFC)
myBuffer = myBufferCursor.next()#setup PDF doc
Author = getpass.getuser() #gets OS user name
pdf_file_name = myNewBusiness.POSTCODE
pdf_file = myReportFolder + os.sep + pdf_file_name + ".pdf"doc = SimpleDocTemplate(pdf_file, pagesize=A4)#array of report elements
parts = []#
#HEADER
#
parstyle = ParagraphStyle(name='Title', fontName='Helvetica', fontSize=12, alignment=1, spaceAfter=20)
p = Paragraph('<b><u>Report</u></b>', parstyle)
parts.append(p)p = Paragraph('The following community groups are located within <b><u>' + myBuffer.Buffer + 'metres  of -  <b><u>' + myNewBusiness.Postcode + '<\b><\u>.',parstyle)
parts.append(p)#
#TABLE
#while myBuffer:parstyle = ParagraphStyle(name='Title', fontName='Helvetica', fontSize=11, alignment=0, spaceAfter=15, spaceBefore =15)p = Paragraph('<b><u>Community groups within ' + myBuffer.Buffer + ' metres of ' + myNewBusiness.Postcode  + '</u><\b>', parstyle)parts.append(p)data = []parstyle = ParagraphStyle(name='Title', fontName='Helvetica', fontSize=11, alignment=0)    while mySelectedGroups:selectedGroups_desc = 'Community ID:'+ '<i>' + mySelectedGroups.Community_ID + '<\i>' + 'Organisation Name :' + mySelectedGroups.Organisation_Name  p = Paragraph(selectedGroups_desc, parstyle)mySelectedGroups = mySelGroupsCursor.next()#build and format tablet=Table(data, colWidths=(100,100))t.setStyle(TableStyle([('ALIGN',(0,0),(-1,-1),'CENTER'),('VALIGN',(0,0),(-1,-1),'TOP'),('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),('BOX', (0,0), (-1,-1), 0.25, colors.black),('TOPPADDING', (0,0), (-1,-1), 10),]))parts.append(t)myBuffer = myBufferCursor.next()#Footnote
parstyle = ParagraphStyle(name='Title', fontName='Helvetica', fontSize=11, alignment=0, spaceAfter=15, spaceBefore=15)
p = Paragraph('''Should you have any further questions **''', parstyle)
parts.append(p)#Build document
doc.build(parts)del mySelGroupsCursor, myNewBusinessCursor, myBufferCursor#Open document
if OpenPDF == "true":os.startfile(pdf_file)

The error that I get is:

Traceback (most recent call last):File "C:\Python25\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScriptexec codeObject in __main__.__dict__File "H:\Report_v2.py", line 100, in <module>t=Table(data, colWidths=(100,100))File "C:\Python25\Lib\site-packages\reportlab\platypus\tables.py", line 236, in __init__raise ValueError("%s must have at least a row and column" % self.identity())
ValueError: <Table@0x010F4710 0 rows x unknown cols>... must have at least a row and column

Any ideas what I need to do as I am struggling a bit.

Thanks.

Answer

From the error and the code, the problem appears to be that data is an empty array []. ReportLab seems to choke on this, so you just need to actually supply some data to the table t and it should work.

https://en.xdnf.cn/q/118196.html

Related Q&A

Secure login with Python credentials from user database

I like to create a secure login with Python but need to check the user table from a database, so that multiple users can log in with their own password. Mainly like this, works like a charm but not sec…

count number of names in list in python [duplicate]

This question already has answers here:How to count the frequency of the elements in an unordered list? [duplicate](33 answers)Closed 6 years ago.i have one list wich has names in it:names = [test,hal…

tensorflow logits and labels must be same size

Im quite new to tensorflow and python, and currently trying to modify the MNIST for expert tutorial for a 240x320x3 image. I have 2 .py scripttfrecord_reeader.pyimport tensorflow as tf import numpy as…

How to call an action when a button is clicked in Tkinter

I am experimenting with Tkinter for the first time, and am trying to call a function when a button is clicked. This is part of my code. mt is referring to a label that I have made dynamic by attachin…

Access range of elements from an array Python

Considering the following dataset:>>> data[:10] array([(T, 2, 8, 3, 5, 1, 8, 13, 0, 6, 6, 10, 8, 0, 8, 0, 8),(I, 5, 12, 3, 7, 2, 10, 5, 5, 4, 13, 3, 9, 2, 8, 4, 10),(D, 4, 11, …

Python - Remove extended ascii

Okay, so I am new to the whole python world so bear with me. Background: We are trying to offload logs into mongo to be able to query and search for them quicker. The device already prints them in a de…

Selenium - Python - Select dropdown meun option - No ID or Name

I am trying to select and element in a dropdown menu:The HTML is:<div class="col-lg-6"><select data-bind="options: indicator_type_list,value: indicatorType,optionsCaption: Choos…

How to prevent triples from getting mixed up while uploading to Dydra programmatically?

I am trying to upload some data to Dydra from a Sesame triplestore I have on my computer. While the download from Sesame works fine, the triples get mixed up (the s-p-o relationships change as the obje…

Adding a new row to a dataframe in pandas for every iteration

Adding a new row to a dataframe with correct mapping in pandasSomething similar to the above question.carrier_plan_identifier ... hios_issuer_identifier 1 AU…

Twisted client protocol - attaching an interface frontend

I am following the tutorial on writing a client/server pair in Twisted located here:http://twistedmatrix.com/documents/current/core/howto/clients.htmlI have everything working for the communication of …