ReportLab段落和文本格式

2024-10-06 13:00:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是,当使用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"))

Tags: fromimportifpdflib格式partsreportlab
3条回答

坏死答案:您要寻找的是字体映射,它告诉ReportLab当用html标记指定黑体和斜体时,在字体系列中使用什么字体。否则,在使用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',)

我希望这就是你想要的:)

# 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)

有几点需要注意:
一。summaryName.build()的参数应该是一个列表。
2。Paragraph()的第一个参数是字符串,而不是列表。
三。简单地编写msgStr.replace('\n','<;br/>;')不会修改msgStr。因此您需要分配它。
您可以参考这些Mouse vs PythonDocs来了解有关ReportLab的更多信息。

相关问题 更多 >