gpt4 book ai didi

Python ReportLab 段落计数打印的行数

转载 作者:行者123 更新时间:2023-12-05 05:11:21 30 4
gpt4 key购买 nike

有什么方法可以在 reportlab 中获取 Flowable Paragraph 的行数?我有一个很长的字符串,用不同的大小和字体打印。我需要知道要使用 TA_JUSTIFY 对齐打印的整个段落使用了多少行。

这可以做到吗?

下面是我的示例 python 文件

import os
import sys
import string
import pprint
import imp
import tempfile

from reportlab.pdfgen import canvas
from reportlab.platypus import Preformatted, XPreformatted, Paragraph, Frame, Image, \
Table, TableStyle, Spacer
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib import styles
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.pagesizes import *
from reportlab.lib import colors
import reportlab.rl_config
# Import as may be needed if we require embedded true type fonts
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.fonts import addMapping
from reportlab.graphics.barcode import code39, code128, code93
from reportlab.graphics.barcode import common
from reportlab.graphics.barcode import qr
from reportlab.graphics import renderPDF
from reportlab.graphics.shapes import Drawing
import string
import os
import imp
from reportlab.lib import colors


canv = canvas.Canvas('Output.pdf')


styles = getSampleStyleSheet()
parastyle = ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)
parastyle.leading = 12
parastyle.fontSize = 11
styles.add(parastyle)

drawText = "The Avengers become divided, both over how to approach Loki and the revelation that S.H.I.E.L.D. plans to harness the Tesseract to develop weapons as a deterrent against hostile extraterrestrials. As the group argues, Barton and Loki's other possessed agents attack the Helicarrier, disabling one of its engines in flight and causing Banner to transform into the Hulk. Stark and Rogers work to restart the damaged engine, and Thor attempts to stop the Hulk's rampage. Romanoff reluctantly fights Barton, and knocks him unconscious, breaking Loki's mind control. Loki escapes after killing Coulson and ejecting Thor from the airship, while the Hulk falls to the ground after attacking a S.H.I.E.L.D. fighter jet. Fury uses Coulson's death to motivate the Avengers into working as a team. Stark and Rogers realize that for Loki, simply defeating them will not be enough; he needs to overpower them publicly to validate himself as ruler of Earth. Loki uses the Tesseract, in conjunction with a device Selvig built, to open a wormhole above Stark Tower to the Chitauri fleet in space, launching his invasion."

inch = INCH = 72
cm = CM = inch/2.54
mm = MM = cm/10


x=10*mm
y=240*mm
width=190*mm
height=10*mm

canv.saveState()
canv.translate(x,y)
canv.rotate(0)
canv.translate(-x,-y)

p = Paragraph(drawText, styles["Justify"])

p.wrapOn(canv, width, height)
p.drawOn(canv, x, y)

canv.showPage()
canv.save()

这是当前输出 Output

我需要获取段落中打印的行数。在我的示例中,我必须得到 11。

如果我更改字体和字体大小,我必须相应地获取值。

最佳答案

调用 Paragraph.wrapParagraph.wrapOn 后,您有一个名为 blPara 的属性可用,它有一个名为 lines 这是一个列表。你可以使用它的长度。像这样:

from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import mm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Paragraph

canvas = Canvas("test.pdf")
drawText = "The Avengers become divided, both over how to approach Loki and the revelation that S.H.I.E.L.D. plans to harness the Tesseract to develop weapons as a deterrent against hostile extraterrestrials. As the group argues, Barton and Loki's other possessed agents attack the Helicarrier, disabling one of its engines in flight and causing Banner to transform into the Hulk. Stark and Rogers work to restart the damaged engine, and Thor attempts to stop the Hulk's rampage. Romanoff reluctantly fights Barton, and knocks him unconscious, breaking Loki's mind control. Loki escapes after killing Coulson and ejecting Thor from the airship, while the Hulk falls to the ground after attacking a S.H.I.E.L.D. fighter jet. Fury uses Coulson's death to motivate the Avengers into working as a team. Stark and Rogers realize that for Loki, simply defeating them will not be enough; he needs to overpower them publicly to validate himself as ruler of Earth. Loki uses the Tesseract, in conjunction with a device Selvig built, to open a wormhole above Stark Tower to the Chitauri fleet in space, launching his invasion."

availWidth, availHeight = 190*mm, A4[1]

style = ParagraphStyle("justifies", alignment=TA_JUSTIFY, fontSize=11, leading=12)
par = Paragraph(drawText, style=style)

par.wrap(availWidth, availHeight)
par.drawOn(canvas, 10*mm, A4[1]-10*mm-par.height)

print(len(par.blPara.lines)) # 11
canvas.save()

还有 simpleSplit 做同样的工作,还有一个额外的好处,那就是更容易获取每一行的文本。

from reportlab.lib.utils import simpleSplit
lines = simpleSplit(drawText, style.fontName, style.fontSize, availWidth)

关于Python ReportLab 段落计数打印的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55530464/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com