Печатные строки счетчика абзацев Python ReportLab - PullRequest
1 голос
/ 05 апреля 2019

Есть ли способ получить количество строк для плавающего абзаца в reportlab?У меня очень длинная строка, напечатанная в разных размерах и шрифтах.Мне нужно знать, сколько строк используется для вывода всего абзаца с выравниванием 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()

Это текущий вывод Вывод

Мне нужно получить количество строк, напечатанных в абзаце.В моем примере я должен получить 11.

Если я изменю шрифт и размер шрифта, я должен получить значение соответственно.

1 Ответ

0 голосов
/ 07 июля 2019

Я не думаю, что в настоящее время это вообще возможно в Reportlab, поскольку переменная не «знает» о контенте напрямую. См. Также этот обмен в списке рассылки reportlab .

...