ReportLab 段落和文本格式

ReportLab Paragraph and text formatting

我的问题是,当使用 reportlab 生成简单的文本文档时,它会丢失所有格式。我已经 运行 尝试调试了几次,问题似乎是,当将 msgStr 传递给 Paragraph 时,它会丢失所有随它发送的格式。

有谁知道如何在保持当前文本格式的同时生成简单的 pdf

代码:

# PDF GENERATION LIBRARIES
# import the report lab PDF generation tools
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
Parts = []

def sumFile(msgStr = None, COMPLETE = 0):

    pdfmetrics.registerFont(TTFont('Inconsolata', 'Inconsolata-Regular.ttf'))

    summaryName = SimpleDocTemplate(vehID+".pdf")

    style = ParagraphStyle(
        name='Normal',
        fontName='Inconsolata',
        fontSize=8,
    )

    msgStr.replace('\n','<br />')

    if msgStr == "PageBreak":
        parts.append(PageBreak())
    else:
        parts.append(msgStr)

    if COMPLETE == 1:
        genStr = "Generated using " + progName + " " + str(progVers)
        parts.append(genStr)
        print parts
        summaryName.build(Paragraph(parts, style))

if __name__ == "__main__":    
    sumFile("%9s %s\n" % ("Bobby", "Sue"))
    sumFile("{0:12}{1:7}{2:5}deg_C\tsmp {3}\n".format("20", "1000", "3.0", "535"))
    sumFile("{0} {1}\n\n".format("09/06/2016", "11:51:39"))

希望这就是您要找的:)

# PDF GENERATION LIBRARIES
# import the report lab PDF generation tools
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

parts = []
msg = ''
progName = "PDF"
progVers = "1.0"
vehID = "vehID"

def sumFile(msgStr = None, COMPLETE = 0):

    global parts, msg, progName, progVers, vehID

    pdfmetrics.registerFont(TTFont('Inconsolata', 'Inconsolata-Regular.ttf'))

    style = ParagraphStyle(
        name='Normal',
        fontName='Inconsolata',
        fontSize=8,
    )

    msgStr = msgStr.replace(' ','&nbsp;')
    msgStr = msgStr.replace('\n','<br />')
    msgStr = msgStr.replace('\t','&nbsp;&nbsp;&nbsp;&nbsp;')

    if msgStr == "PageBreak":
        if msg != '':
            parts.append(Paragraph(msg, style = style))
            msg = ''
        parts.append(PageBreak())
    else:
        msg += msgStr

    if COMPLETE == 1:
        if msg != '':
            parts.append(Paragraph(msg, style = style))
            msg = ''
        genStr = "Generated using " + progName + " " + str(progVers)
        parts.append(Paragraph(genStr, style = style))
        summaryName = SimpleDocTemplate(vehID+".pdf")
        summaryName.build(parts)

if __name__ == "__main__":    
    sumFile("%9s %s\n" % ("Bobby", "Sue"))
    sumFile("{0:12}{1:7}{2:5}deg_C\tsmp {3}\n".format("20", "1000", "3.0", "535"))
    sumFile("{0} {1}\n\n".format("09/06/2016", "11:51:39"))
    # sumFile("{0} {1}\n\n".format("09/06/2016", "11:51:39"), COMPLETE=1)

一些注意事项:
1. summaryName.build() 的参数应该是一个列表。
2. Paragraph() 的第一个参数是字符串而不是列表。
3. 简单的写msgStr.replace('\n','
')不会修改msgStr。因此你需要分配它。
您可以参考这些 Mouse vs Python, Docs 以了解有关 ReportLab 的更多信息。

Necro-answer:您正在寻找的是字体映射,当使用 html 标记指定粗体和斜体时,它会告诉 ReportLab 在字体系列中使用什么字体。否则,当使用 TrueType 字体时,ReportLab 将不会应用格式。

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.fonts import addMapping

pdfmetrics.registerFont(TTFont(font, 'Times New Roman.ttf'))
pdfmetrics.registerFont(TTFont(font, 'Times New Roman Italic.ttf'))
pdfmetrics.registerFont(TTFont(font, 'Times New Roman Bold.ttf'))
pdfmetrics.registerFont(TTFont(font, 'Times New Roman Bold Italic.ttf'))

# 2nd positional param is bool flag for italic
# 3rd positional param is bool flag for boldface
addMapping('Times New Roman', 0, 0, 'Times New Roman')
addMapping('Times New Roman', 0, 1, 'Times New Roman Italic')
addMapping('Times New Roman', 1, 0, 'Times New Roman Bold')
addMapping('Times New Roman', 1, 1, 'Times New Roman Bold Italic')

现在您可以使用 <strong><em>(如果您愿意,也可以使用 <b><i>),所有内容都将按照您的预期进行格式化。

在我的 windows 系统上,我必须找到真正的字体文件名,然后按如下方式使用它们。 现在我的段内 bold 可以正常工作了。

    pdfmetrics.registerFont(TTFont('Times', 'times.ttf',))
    pdfmetrics.registerFont(TTFont('Timesi', 'timesi.ttf',))
    pdfmetrics.registerFont(TTFont('Timesbd', 'timesbd.ttf',))
    pdfmetrics.registerFontFamily('Times',normal='Times',bold='Timesbd',
    italic='Timesi',)