ReportLab段落和文本格式

时间:2016-06-09 10:58:11

标签: python pdf reportlab 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"))

3 个答案:

答案 0 :(得分:3)

我希望这就是你要找的东西:)

# 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','&lt; br /&gt;')不会修改msgStr。因此你需要分配它 您可以引用这些Mouse vs PythonDocs来详细了解ReportLab。

答案 1 :(得分:1)

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

<td class="ClubRow" width="80%">
   <div>
       <a href="/rankings/club.aspx?ClubID=27086" class="ClubLink">AYSO United</a></div>
   <div class="SubHeading">Madison, AL</div>
       <a href="http://www.aysounitednorthalabama.org" target="_blank"><img src="/images/icons/ArrowRightSm.png" class="LinkIcon"><font color="black">www.aysounitednorthalabama.org</font></a>
</td>

现在,您可以使用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>(如果您愿意),并且所有内容都会按照您的预期进行格式化。

答案 2 :(得分:1)

在我的Windows系统上,我必须找到真正的字体文件名,然后使用它们,如下所示。 现在我的段内粗体正常工作。

    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',)