Skip to main content
 首页 » 编程设计

python之Reportlab 文本背景大小与字体大小不匹配

2025年05月04日13落叶无声

我正在尝试在一个简单的黑色栏上添加一个带有白色文本的标题(包含页面中的内容,因此制作一个实际的标题非常复杂)。问题是文本的背景似乎不随文本缩放,如我的 MWI 所示:

from reportlab.lib import colors 
from reportlab.lib.enums import TA_JUSTIFY, TA_RIGHT, TA_CENTER, TA_LEFT 
from reportlab.lib.pagesizes import letter, landscape 
from reportlab.platypus import PageTemplate, Frame, NextPageTemplate, BaseDocTemplate, SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, PageBreak 
from reportlab.platypus import ListFlowable, ListItem 
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 
from reportlab.lib.units import inch 
from reportlab.rl_config import defaultPageSize 
from reportlab.pdfbase.pdfmetrics import stringWidth 
from reportlab.pdfgen import canvas 
 
#c = canvas.Canvas("tables.pdf") 
doc = SimpleDocTemplate("mwi.pdf",pagesize=letter, 
                        rightMargin=72,leftMargin=72, 
                        topMargin=72,bottomMargin=60) 
 
styles=getSampleStyleSheet() 
styles.add(ParagraphStyle(name='Table Top Black Back', fontName ='Helvetica',fontSize=14, backColor = colors.black, textColor=colors.white, alignment=TA_LEFT)) 
styles.add(ParagraphStyle(name='Table Top Red Back', fontName ='Helvetica',fontSize=9, backColor = colors.red, textColor=colors.black, alignment=TA_LEFT)) 
 
styleN = styles["BodyText"] 
 
# Header 
# report: topic/subtopic overview 
report = [] 
ptext = 'Test'  
report.append(Paragraph(ptext, styles["Table Top Black Back"])) 
report.append(Spacer(1, 24)) 
 
ptext = 'Test'  
report.append(Paragraph(ptext, styles["Table Top Black Back"])) 
report.append(Spacer(1, 24)) 
 
ptext = 'Test'  
report.append(Paragraph(ptext, styles["Table Top Red Back"])) 
report.append(Spacer(1, 48)) 
 
 
# Build Document 
doc.build(report) 

请您参考如下方法:

首先,从你的代码来看,它不会编译,因为它给出了错误。

ImportError: cannot import name ListFlowable 
ImportError: cannot import name ListItem 

我删除了这些的导入,因为这里不需要。

背景大小不匹配的原因是因为您没有在段落中指定“leading”属性,您更改了“fontSize”,但没有更改“leading”属性。

这个“领先”是什么? 这是相邻文本行之间的间距;一个好的经验法则是使它比磅值大 20%。要获得双倍行距的文本,请使用高行距。

默认情况下,字体大小设置为 10,行距设置为 12。

所以在上面的示例中,一旦将 fontSize 设置为 14,但行距仍为 12,这就是文本大小不合适的原因。

解决方案是在上面的示例中定义一个稍大的前导。

我不认为这是一个错误,而是一个不理想的设计考虑。这是一个非常主观的话题,不确定 reportlab-developer 的那个时候发生了什么。

from reportlab.lib import colors 
from reportlab.lib.enums import TA_JUSTIFY, TA_RIGHT, TA_CENTER, TA_LEFT 
from reportlab.lib.pagesizes import letter, landscape 
from reportlab.platypus import PageTemplate, Frame, NextPageTemplate, BaseDocTemplate, SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, PageBreak 
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 
from reportlab.lib.units import inch 
from reportlab.rl_config import defaultPageSize 
from reportlab.pdfbase.pdfmetrics import stringWidth 
from reportlab.pdfgen import canvas 
 
#c = canvas.Canvas("tables.pdf") 
doc = SimpleDocTemplate("mwi.pdf",pagesize=letter, 
                    rightMargin=72,leftMargin=72, 
                    topMargin=72,bottomMargin=60) 
 
styles=getSampleStyleSheet() 
styles.add(ParagraphStyle(name='Table Top Black Back', fontName ='Helvetica',fontSize=14, leading=16,backColor = colors.black, textColor=colors.white, alignment=TA_LEFT)) 
styles.add(ParagraphStyle(name='Table Top Red Back', fontName ='Helvetica',fontSize=9, leading=12, backColor = colors.red, textColor=colors.black, alignment=TA_LEFT)) 
 
styleN = styles["BodyText"] 
 
# Header 
# report: topic/subtopic overview 
report = [] 
ptext = 'Test'  
report.append(Paragraph(ptext, styles["Table Top Black Back"])) 
report.append(Spacer(1, 24)) 
 
ptext = 'Test'  
report.append(Paragraph(ptext, styles["Table Top Black Back"])) 
report.append(Spacer(1, 24)) 
 
ptext = 'Test'  
report.append(Paragraph(ptext, styles["Table Top Red Back"])) 
report.append(Spacer(1, 48)) 
 
# Build Document 
doc.build(report) 

希望这对您有所帮助。快乐的报告实验室编码。