【问题标题】:ReportLab - Not writing the records that have been looped and storedReportLab - 不写已经循环和存储的记录
【发布时间】:2012-04-12 13:50:32
【问题描述】:

question i posted yesterday的后续内容

我能够遍历我的所有记录,并将其写入一个名为 data 的变量,然后将其附加到另一个名为 parts 的变量上,当我在 part 变量上观察时,我可以看到正确的数据,但是当它尝试构建pdf时...

doc.build(parts)

我收到以下错误

AttributeError: 'list' object has no attribute 'getKeepWithNext'

这是完整的代码

import os, arcgisscripting, datetime,  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:\HeartOfTheCity.gdb"
myReportFolder = r"W:\Reports"
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
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>Heart of the City Search 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)

#Enter while loop for each buffer feature
#while myBuffer:
while myBuffer:
    print myBuffer.Buffer
    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><br>', 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 + '<br>'
        p = Paragraph(selectedGroups_desc, parstyle)
        data.append(p)
        #print data

        mySelectedGroups = mySelGroupsCursor.next()    
    parts.append(data)
    print parts
    #print parts

    myBuffer = myBufferCursor.next()

#
#FOOTER
#
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)

doc.build(parts)

del mySelGroupsCursor, myNewBusinessCursor, myBufferCursor

#Open document
if OpenPDF == "true":
    os.startfile(pdf_file)

有什么想法吗?

【问题讨论】:

  • 我对此投了反对票,因为这是一个相当开放的问题(“有什么想法吗?”),而且显然不错的答案没有被接受。
  • "Any Ideas" = 有人知道我为什么会遇到这个问题!

标签: python reportlab


【解决方案1】:

自从我使用 ReportLab 以来已经有一段时间了,所以我可能会在这方面有所欠缺。但是,我认为 parts 中的所有项目都需要属于 Flowable 类。在这里,您将列表附加到部件:

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 + '<br>'
    p = Paragraph(selectedGroups_desc, parstyle)
    data.append(p)
    #print data

    mySelectedGroups = mySelGroupsCursor.next()    
parts.append(data)

我不认为这是有效的。部分中的所有内容都应该是Flowable。也许您正在尝试制作一个列表,在这种情况下您需要一个 ListFlowable。

这是来自文档http://www.reportlab.com/docs/reportlab-userguide.pdf 第 9.10 节的示例:

from reportlab.platypus import ListFlowable, ListItem
from reportlab.lib.styles import getSampleStyleSheet
styles = getSampleStyleSheet()
style = styles["Normal"]
t = ListFlowable(
[
Paragraph("Item no.1", style),
ListItem(Paragraph("Item no. 2", style),bulletColor="green",value=7),
ListFlowable(
                [
                Paragraph("sublist item 1", style),
                ListItem(Paragraph('sublist item 2', style),bulletColor='red',value='square')
                ],
                bulletType='bullet',
                start='square',
                ),
Paragraph("Item no.4", style),
],
bulletType='i'
)

【讨论】:

  • 谢谢汉斯,我如何制作一个可流动的列表?
  • 我添加了一个例子。您应该查看文档,因为它们显示了代码输出的内容,而我无法在答案中完全重现。
  • 不幸的是,我不能使用 ListFlowable,因为根据这个答案,它在我的版本中不存在..stackoverflow.com/questions/6715530/…,我还想如何创建一个循环和写入的列表?
  • 也许这会有所帮助stackoverflow.com/questions/748881/…
  • 另外,如果您查看 ReportLab 的每晚构建,他们有 ListFlowable 类。不管出于什么原因,他们已经两年没有发布新的稳定版本了。
猜你喜欢
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
相关资源
最近更新 更多