2016-06-09 218 views
1

我的问题是,当使用reportlab生成一个简单的文本文档时,它会丢失所有的格式。我已经运行了几次来尝试和调试它,并且问题似乎是,当通过msgStrParagraph时,它会丢失与它一起发送的所有格式。ReportLab段落和文本格式

有谁知道如何生成一个简单的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")) 

回答

1

我希望这是你在找什么:)

# 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','< br/>')不会修改msgStr。因此你需要分配它。
您可以参考这些Mouse vs PythonDocs以了解有关ReportLab的更多信息。

0

Necro-answer:您正在寻找的是字体映射,它告诉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>如果你喜欢),一切都将被格式化为您期望。

0

在我的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',)