报表实验室文本背景大小与字体大小不匹配



我正在尝试在一个简单的黑条上创建一个包含白色文本的页眉(包含页面中的内容,所以实际的页眉要复杂得多)。问题是文本的背景似乎与文本不相称,正如我的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

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

背景大小不匹配的原因是您没有在段落中指定"前导"属性,您更改了"fontSize",但没有更改"前导"特性。

这是什么"领先"?这是相邻文本行之间的间距;一个很好的经验法则是使这比点的大小大20%。要获得双倍行距的文本,请使用高前导。

默认情况下,fontSize设置为10,前导设置为12。

因此,在上面的示例中,一旦您将fontSize设置为14,但前导值仍然为12,这就是文本大小不合适的原因。

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

我不认为这是一个错误,而是一个不优化的设计考虑。这是一个非常主观的话题,不确定reportlab开发人员当时正在经历什么。

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)

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