ReportLab - 创建表时出错 [英] ReportLab - error when creating a table

查看:56
本文介绍了ReportLab - 创建表时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 ReportLab,我尝试编辑一个现有的脚本来完成我想做的事情,但是当我尝试运行该脚本时出现以下错误.

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.

脚本 -

import os, arcgisscripting, datetime, getpass, string

from reportlab.lib.pagesizes import A4
from reportlab.platypus import *
from reportlab.lib import colors
from reportlab.lib.styles import ParagraphStyle

myWorkspace = 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 table
    t=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)

我得到的错误是:

Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec 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.

谢谢.

推荐答案

从错误和代码来看,问题似乎是data为空数组[].ReportLab 似乎对此感到窒息,所以您只需要实际向表 t 提供一些数据,它应该可以工作.

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.

这篇关于ReportLab - 创建表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆