gpt4 book ai didi

python - 调整 reportlab.platypus.Paragraph 是否可流动?

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:05 30 4
gpt4 key购买 nike

我有以下代码尝试调整 reportlab Platypus flowable 文本的字体大小,直到它适合我给它的可用高度。但是我发现 Paragraph flowable 没有在递归的每个循环中保存它的 style.fontSize 属性,并且 python 阻止了无限递归。

def fits_space(w,h,aW,aH,p):
"""
Determines if inputted text fits in the space given for a paragraph
and if it doesn't, reduces the font-size until it does.

"""
if w<=aW and h<=aH:
# return font size to apply it to the para again
print "final font_size: %d" % p.style.fontSize
return p # now render the paragraph in the doctemplate
else:
p.style.fontSize -= 1
w,h = p.wrap(aW, aH)
fits_space(w,h,aW,aH,p)

def renderPage(name, text):
doc = SimpleDocTemplate("%s.pdf" % name)
parts = []
style = ParagraphStyle(name='fancy')
style.fontSize = 150
p = Paragraph(text, style)
aW = PAGE_WIDTH-4*inch # available width and height
aH = PAGE_HEIGHT-4*inch
w,h = p.wrap(aW, aH) # find required space
p = fits_space(w,h,aW,aH,p) # recursively fit the font size to the text
parts.append(p)
doc.build(parts)

谁能告诉我为什么 - 在 fits_space() 函数中,在 else 子句中,当我调用 p.wrap(aW, aH) 时,输出值与我将段落的字体大小减 1 之前相同?如果我减小字体大小,包裹的高度肯定会更小吗?

有什么地方出错了吗?

更新下面 Nitzie 的代码几乎可以工作只需要向 style.leading 添加一个调整器,在我的例子中也是如此:

def shrink_font_size(aW, aH, text, style):
"""Shrinks font size by using pdfmetrics to calculate the height
of a paragraph, given the font name, size, and available width."""
def break_lines(text, aW):
# simpleSplit calculates how reportlab will break up the lines for
# display in a paragraph, by using width/fontsize.
return simpleSplit(text, style.fontName, style.fontSize, aW)

def line_wrap(lines, style):
# Get overall width of text by getting stringWidth of longest line
width = stringWidth(max(lines), style.fontName, style.fontSize)
# Paragraph height can be calculated via line spacing and number of lines.
height = style.leading * len(lines)
return width, height

lines = break_lines(text, aW)
width, height = line_wrap(lines, style)

while height > aH or width > aW:
style.fontSize -= 1
style.leading -= 1 # do this if you're leading is based on the fontSize
lines = break_lines(text, aW)
width, height = line_wrap(lines, style)

TIA

最佳答案

出于某种原因,p.wrap 似乎并没有真正重新计算高度/宽度,一旦您在调用它一次后更改了字体大小。

我能够通过根据 ReportLab 的 stringWidthsplitLines 方法计算字体大小来获得一个工作版本。尽管这意味着它不再是递归的:

from reportlab.platypus import SimpleDocTemplate
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import A4
from reportlab.platypus import Paragraph
from reportlab.lib.utils import simpleSplit
from reportlab.pdfbase.pdfmetrics import stringWidth

def shrink_font_size(aW, aH, text, style):
"""Shrinks font size by using pdfmetrics to calculate the height
of a paragraph, given the font name, size, and available width."""
def break_lines(text, aW):
# simpleSplit calculates how reportlab will break up the lines for
# display in a paragraph, by using width/fontsize.
return simpleSplit(text, style.fontName, style.fontSize, aW)

def line_wrap(lines, style):
# Get overall width of text by getting stringWidth of longest line
width = stringWidth(max(lines), style.fontName, style.fontSize)
# Paragraph height can be calculated via line spacing and number of lines.
height = style.leading * len(lines)
return width, height

lines = break_lines(text, aW)
width, height = line_wrap(lines, style)

while height > aH or width > aW:
style.fontSize -= 1
lines = break_lines(text, aW)
width, height = line_wrap(lines, style)


def renderPage(name, text):
doc = SimpleDocTemplate("%s.pdf" % name)
parts = []
# Wasn't sure where PAGE_WIDTH and PAGE_HEIGHT came from for OP,
# so I just used a standard A4 page measurement.
PAGE_WIDTH, PAGE_HEIGHT = A4
aW = PAGE_WIDTH - 4*inch # available width and height
aH = PAGE_HEIGHT - 4*inch

style = ParagraphStyle(name='fancy')
style.fontSize = 200
style.leading = 20
shrink_font_size(aW, aH, text, style)

p = Paragraph(text, style)
parts.append(p)
doc.build(parts)

if __name__ == "__main__":
renderPage('test', '12345689019283382848248284 842828428529392381472754 842828428529392381472754 842828428529392381472754\n' * 10)

关于python - 调整 reportlab.platypus.Paragraph 是否可流动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14719194/

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