2016-06-09 221 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',)