第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Python ReportLab 段落計(jì)數(shù)打印行數(shù)

Python ReportLab 段落計(jì)數(shù)打印行數(shù)

寶慕林4294392 2021-12-29 20:07:29
有什么方法可以讓我在 reportlab 中獲得 Flowable Paragraph 的行數(shù)?我有一個(gè)用不同大小和字體打印的很長(zhǎng)的字符串。我需要知道使用 TA_JUSTIFY 對(duì)齊方式打印的整個(gè)段落使用了多少行。這能做到嗎?下面是我的示例 python 文件import osimport sysimport stringimport pprintimport impimport tempfilefrom reportlab.pdfgen import canvasfrom reportlab.platypus import Preformatted, XPreformatted, Paragraph, Frame, Image, \     Table, TableStyle, Spacerfrom reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFYfrom reportlab.lib import stylesfrom reportlab.lib.styles import getSampleStyleSheet, ParagraphStylefrom reportlab.lib.pagesizes import *from reportlab.lib import colorsimport reportlab.rl_config# Import as may be needed if we require embedded true type fontsfrom reportlab.pdfbase import pdfmetricsfrom reportlab.pdfbase.ttfonts import TTFontfrom reportlab.lib.fonts import addMappingfrom reportlab.graphics.barcode import code39, code128, code93from reportlab.graphics.barcode import commonfrom reportlab.graphics.barcode import qrfrom reportlab.graphics import renderPDFfrom reportlab.graphics.shapes import Drawingimport stringimport osimport impfrom reportlab.lib import colorscanv = canvas.Canvas('Output.pdf')styles = getSampleStyleSheet()parastyle = ParagraphStyle(name='Justify', alignment=TA_JUSTIFY)parastyle.leading = 12parastyle.fontSize = 11styles.add(parastyle)我需要獲取段落中打印的行數(shù)。在我的例子中,我必須得到 11。如果我更改字體和字體大小,我必須相應(yīng)地獲取值。
查看完整描述

2 回答

?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

在你調(diào)用Paragraph.wrapor 之后Paragraph.wrapOn,你有一個(gè)叫做blParaavailable的屬性,它有一個(gè)叫做lines列表的屬性。你可以使用它的長(zhǎng)度。像這樣:


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)


查看完整回答
反對(duì) 回復(fù) 2021-12-29
?
臨摹微笑

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊

我剛剛遇到了這個(gè)問(wèn)題,同時(shí)也在尋找一些簡(jiǎn)單的方法來(lái)完成您的要求。不幸的是我沒(méi)有找到答案,所以我試圖把一些東西放在一起來(lái)讓它工作。它不漂亮,但它有效!


我剛剛在您的 canv.save() 方法調(diào)用之后添加了此代碼:


pixelRatio = 1.333333 # 1 point = 1.333333 pixels

# https://websemantics.uk/articles/font-size-conversion/

# http://www.endmemo.com/sconvert/pixelpoint.php#targetText=Pixel%E2%86%94Point%201%20Point,Twip%201%20Pixel%20%3D%2015%20Twip


fontLineHeight = styles["Justify"].fontSize * pixelRatio

boxWidth = width

boxHeight = height

boxLines = int(72/fontLineHeight)

boxLRBuffer = 0 # Adjust this to whatever inside margin/padding you have in your box

boxPixelCapacity = boxWidth * boxLines - boxLRBuffer # Total pixels in the box


# A list to hold the lines of text

textLines = []

# A pointer to indicate which character is being evaluated

position = len(drawText)

# Make a copy of the original string so as to not modify it

tempString = drawText

# This is a dummy string used to test the width of the string

testString1 = tempString

# Another dummy string to hold what is not being split into a line

# testString2 will be build going backwards from the last character

# to the character right after the line split

testString2 = ''

# Boolean flag to get out of the while loop

done = False

while not done:

    # Use pdfmetrics to get the line width in points based upon the fontName and fontSize used

    if pdfmetrics.stringWidth(testString1, styles["Justify"].fontName, styles["Justify"].fontSize, 'utf8') > boxWidth-boxLRBuffer*2:

        # Since the line is too long, move backwards through the string until an appropriate size is found

        # by decreasing the pointer position.  Spaces also take space.

        position -= 1

        if position >= 0:

            #since the the pointer has not reached the beginning of testString1

            testString1 = testString1[:position] # remove the last character from testString1

            testString2 = tempString[position:] # add the next character to testString2 for later use

    # Since testString1 now fits within the stringWidth

    # it must be checked to see if this is the last line

    # being processed.

    elif testString1 == testString2:

        # This must be the last line since it equals testString2

            textLines.append(testString2.strip())

            break

    # This is not the last line of text, so now work backwards in

    # the string to find a space so the line can be split

    elif ' ' in testString1:

        while testString1[position:] != ' ' and position >= 0:

            testString1 = testString1[:position]

            testString2 = tempString[position:]

            position -= 1

        if len(testString1):

            textLines.append(testString1.strip())

        else:

            textLines.append(testString2.strip())

            break

        tempString = tempString[position:].strip()

        testString1 = tempString

        position = len(tempString)

    elif testString1 == "":

        # It's done

        break

    else:

        # The text does not exceed the stringWidth

        textLines.append(testString1)

        # testString2 must be checked and if empty then it's done

        if testString2 == "":

            done = True

        else:

           tempString = tempString[position:].strip()

           testString1 = tempString

           position = len(tempString)

print(len(textLines))

我愿意接受使此代碼更好或更精簡(jiǎn)的建議。


查看完整回答
反對(duì) 回復(fù) 2021-12-29
  • 2 回答
  • 0 關(guān)注
  • 348 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)