python - Reportlab text background size doesn't match font size -
i'm trying have header white text on simple black bar (with content page, doing actual header way complicated). problem background of text doesn't seem scale text, seen in mwi:
from reportlab.lib import colors reportlab.lib.enums import ta_justify, ta_right, ta_center, ta_left reportlab.lib.pagesizes import letter, landscape reportlab.platypus import pagetemplate, frame, nextpagetemplate, basedoctemplate, simpledoctemplate, paragraph, spacer, image, table, tablestyle, pagebreak reportlab.platypus import listflowable, listitem reportlab.lib.styles import getsamplestylesheet, paragraphstyle reportlab.lib.units import inch reportlab.rl_config import defaultpagesize reportlab.pdfbase.pdfmetrics import stringwidth 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 reddish back', fontname ='helvetica',fontsize=9, backcolor = colors.red, textcolor=colors.black, alignment=ta_left)) stylen = styles["bodytext"] # header # report: topic/subtopic overview study = [] 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 reddish back"])) report.append(spacer(1, 48)) # build document doc.build(report)
firstly, code, won't compile gave error.
importerror: cannot import name listflowable importerror: cannot import name listitem
i removed import these it's not required here.
the reason why background size not match because did not specify "leading" attribute in paragraph, changed "fontsize", did not alter "leading" attribute.
what "leading"? spacing between adjacent lines of text; rule of thumb create 20% larger point size. double-spaced text, utilize high leading.
by default, fontsize set @ 10 , leading set @ 12.
so in illustration above, set fontsize 14, leading still @ value of 12, reason why text not sized well.
solution defined larger leading in illustration above.
i don't see bug, rather not optimal design consideration. subjective topic, not sure going through reportlab-developer's time.
from reportlab.lib import colors reportlab.lib.enums import ta_justify, ta_right, ta_center, ta_left reportlab.lib.pagesizes import letter, landscape reportlab.platypus import pagetemplate, frame, nextpagetemplate, basedoctemplate, simpledoctemplate, paragraph, spacer, image, table, tablestyle, pagebreak reportlab.lib.styles import getsamplestylesheet, paragraphstyle reportlab.lib.units import inch reportlab.rl_config import defaultpagesize reportlab.pdfbase.pdfmetrics import stringwidth 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 reddish back', fontname ='helvetica',fontsize=9, leading=12, backcolor = colors.red, textcolor=colors.black, alignment=ta_left)) stylen = styles["bodytext"] # header # report: topic/subtopic overview study = [] 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 reddish back"])) report.append(spacer(1, 48)) # build document doc.build(report)
hope helps. happy reportlab coding.
python text reportlab
No comments:
Post a Comment