python - 在 reportlab 中,将尽可能多的空行添加到适合最后一页的长表中

标签 python reportlab

我正在准备一个项目列表的 PDF 报告,它本质上是一个长表,其中一些单元格填充了数据,一些单元格为空。该报告将被打印出来,用户将在纸上填写空白单元格。她还将添加新行并填写新数据。

所以我会提供一些(例如3个)空行作为网格来填充数据。但是如果页面上还有剩余空间,我想用空行填充它。

如何在最后一页添加尽可能多的行?

不同的方法:
当使用每行有一个单行表的段落时,这种行为可能更容易实现。但我需要在每一页上重复第一行(这在具有 repeatRows=1 的表格中很容易)。

有什么想法吗?

最佳答案

我将不得不去检索我的一些代码,但我似乎记得必须通过测量当前的 X/Y 位置来做事,根据我使用的任何边距计算它,然后确定是否有更多信息可能适合或者如果我需要一个新页面。我的项目是将长文本 block 自动换行,这很相似但不完全相似。我将很快在此处更新一些代码。

def newline(self, options, text = ''):
  if getattr(self, 'lpp', None) == self.lines[self.pages]:
    self.newpage()

  if getattr(self, 'y', None) > self.h - self.bm * inch:
    self.newpage()

在这种情况下,我有可能已设置的 lpp(每页行数)属性,所以我首先检查该值是否存在,如果存在,我是否在当前页面的行数处。如果对每页的总行数没有限制,那么我会测试我的 Y 位置和底部边距是多少。如有必要,在页面中打补丁。这里遗漏了一些内容,但这是一般的想法。

def newline(self, options, text = ''):
  if getattr(self, 'lpp', None) == self.lines[self.pages]:
    self.newpage()

  if getattr(self, 'y', None) > self.h - self.bm * inch:
    self.newpage()

  self.addLine()

  self.putText(self.x, self.h - self.y, text)

def putText(self, x, y, text):
  # If we actually place some text then we want to record that.
  if len(text.strip()) > 0 and not self.hasText[self.pages]:
    self.hasText[self.pages] = True
  # Something here to handle word wrap.
  if self.wrap:
    lines = self._breakScan(text)

    if len(lines) > 1:
      self.c.drawString(x, y, lines[0])

      self.newline('', ' '.join(lines[1:]))
    elif lines:
      self.c.drawString(x, y, lines[0])
  else:
    self.c.drawString(x, y, text)

在这里,self.c 是我的 Canvas 。我一直在跟踪我在页面上放下了多少行,因为有时我们会重新包装可能包含分页符的文档,所有这些都在我们的自定义标记中。

关于python - 在 reportlab 中,将尽可能多的空行添加到适合最后一页的长表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6297258/

相关文章:

python - numpy - pretty-print

python - 如何通过 reportlab 将表格定位在特定的 x 和 y 坐标处

javascript - Django ajax 403 因为 httponly cookie

python - 使用 python-pptx 编辑页眉和页脚

python - Matplotlib 图转 PDF 无需保存

python - ReportLab 有可流动的 matplotlib 吗?

python - Unicode 字符是 Geraldo/ReportLab 生成的 PDF 中的框

python - Reportlab:reportlab中表列的动态颜色(存储在django模型中的颜色)

python linux Selenium : chrome not reachable

python - 如何在命令行的虚拟环境中打印 python 测试中变量的内容