Python 页面模板中的Reportlab动态信息

Python 页面模板中的Reportlab动态信息,python,reportlab,platypus,Python,Reportlab,Platypus,我必须使用ReportLab将长格式文本文档转换为PDF。我的目标是逐页读取文本文件(文本文件中的1页长度可变,由表单提要决定)。然后,对于每个页面,我必须使用页面模板的onPage功能编写页面的静态部分,同时将动态部分插入到pdf文件的一个或多个页面中 问题是在调用文档的build之前,我的onPage函数不会被调用。在阅读文本页面时,动态创建pdf页面的替代解决方案是什么 下面是脚本中试图编写页面静态内容的部分 #Globals formlines = [] header = [] foot

我必须使用
ReportLab
将长格式文本文档转换为PDF。我的目标是逐页读取文本文件(文本文件中的1页长度可变,由表单提要决定)。然后,对于每个页面,我必须使用页面模板的
onPage
功能编写页面的静态部分,同时将动态部分插入到pdf文件的一个或多个页面中

问题是在调用文档的
build
之前,我的
onPage
函数不会被调用。在阅读文本页面时,动态创建pdf页面的替代解决方案是什么

下面是脚本中试图编写页面静态内容的部分

#Globals
formlines = []
header = []
footer = []
detail_lines = []
def processForm():
    # First 15 lines header
    # Then variable number of detail lines
    # Last 15 line footer
  header = formlines[0:14]
  print "Processing form\n"
  footer = formlines[-15:-1]
  detail_lines = formlines[15:-15]

def firstPgCanv(c, doc):
  # Process header
  pract_addr = header[0][0:43] + "\n" + header[1] + "\n" + header[2][0:43]  
  remit_to = header[8][44:] + "\n" + header[9][44:] + "\n" + header[10][44:]
  guar_addr = header[8][1:42] + "\n" + header[9][1:42] + "\n" + header[10][1:42]

  c.saveState()
  c.translate(.3 * inch, 0 * inch)

  tx = c.beginText(.3 * inch,height- .35 * inch)
  tx.textLines(header_addr)
  c.drawText(tx)

  curY = curY - gap 
# Create text object
  tx = c.beginText(.3 * inch, curY * inch)
  tx.textLines(guar_addr)
  c.drawText(tx)

  tx = c.beginText(4.3 * inch, curY * inch)
  tx.textLines(remit_to)
  c.drawText(tx)

  c.restoreState()

def main(argv=None):
  if argv is None:
    argv = sys.argv

  args = sys.argv[1:]

  #Open source file
  src_file  = sys.argv[1]
  dest = sys.argv[2]
  dest_file = os.path.join(os.path.dirname(src_file), dest);

  f=open(src_file, 'r')
  #Document Template
  doc = BaseDocTemplate(dest_file,
                        pagesize=letter,
                        leftMargin=.3*inch,
                        rightMargin= .1 * inch,
                        topMargin= .1 * inch,
                        bottomMargin=.3 * inch,
                        showBoundary=1)

  # Frames
  frameT = Frame(doc.leftMargin + 2*inch, doc.bottomMargin, doc.width - 2.01*inch, doc.height - 4.1*inch, id='normal', showBoundary=0)
  frameB = Frame(doc.leftMargin+2, doc.bottomMargin, 7.5*inch, 10*inch, id='small', showBoundary=1)

  # Page Templates
  doc.addPageTemplates([PageTemplate(id='First',frames=frameT,onPage=firstPgCanv),
                        PageTemplate(id='Later',frames=frameB,onPage=othPgCanv)
                      ])

  Elements = []
  # Process the input file
  while 1:
    ln = f.readline()
    if len(ln) == 0:    # EOF
      break

    s = chomp(ln)

    ff = s.find("\f")    
    if ff != -1:        # \f found along with first line of next form
      frag = s.split("\f")
      final = frag.pop()
      formlines.append(final)
      processForm()
      Elements.append(NextPageTemplate('First'))
      Elements.append(PageBreak)
      # Here I will write few more pages of page Template 'Later'
    else:
      formlines.append(s)

# EOF -- close and flush last page 
  f.close()  
  doc.build(Elements)

if __name__ == "__main__":
    sys.exit(main())

经过一些研究,我得出结论,没有办法动态处理它。剩下的唯一解决办法是将整个文件访问到列表中。然后页面模板的
onPage
函数使用一个全局变量跟踪页面,并根据页面访问适当的列表行