2016-09-07 409 views
1

我必須使用reportlab.pdfbase.pdfmetrics中的registerFontFamily方法。 我試圖將兩種字體添加到家族中,但我無法將粗體字用於「< b> sometext </b>」。我的報告需要這個功能。我只能使用普通的,我不知道爲什麼。reportlab在registerFont中添加字體

這是代碼。

registerFont(TTFont('Own_Font', os.path.dirname(os.path.abspath(__file__)) + '\\OwnSans-Regular.ttf')) 
registerFont(TTFont('OwnBold_Font', os.path.dirname(os.path.abspath(__file__)) + '\\OwnSans-Bold.ttf')) 

registerFontFamily('Own_Font',normal='Own_Font',bold='OwnBold_Font') 

# define parameter for the page and paragraph font 
PAGE_WIDTH, PAGE_HEIGHT = landscape(A4) 
STYLES     = getSampleStyleSheet() 
STYLES.add(ParagraphStyle(name='Text', fontName = 'Own_Font', fontSize = 10)) 

STYLES.add(ParagraphStyle(name='Centered', fontName = 'Own_Font', fontSize = 10, alignment=TA_CENTER)) 
STYLES.add(ParagraphStyle(name='CenteredBig', parent=STYLES['Centered'], fontSize=18, spaceAfter=10)) 
STYLES.add(ParagraphStyle(name='CenteredMedium', parent=STYLES['Centered'], fontSize=15, spaceAfter=10)) 

我已經得到了以下錯誤消息:

File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paragraph.py", line 916, in __init__ 
    self._setup(text, style, bulletText or getattr(style,'bulletText',None), frags, cleanBlockQuotedText) 
    File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paragraph.py", line 938, in _setup 
    style, frags, bulletTextFrags = _parser.parse(text,style) 
    File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paraparser.py", line 1083, in parse 
    self.feed(text) 
    File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\lib\xmllib.py", line 562, in finish_starttag 
    self.handle_starttag(tag, method, attrs) 
    File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\lib\xmllib.py", line 596, in handle_starttag 
    method(attrs) 
    File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paraparser.py", line 823, in start_para 
    self._stack = [self._initial_frag(attr,_paraAttrMap)] 
    File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\platypus\paraparser.py", line 817, in _initial_frag 
    frag.fontName, frag.bold, frag.italic = ps2tt(style.fontName) 
    File "X:\tools\Python2\lib\site-packages\reportlab-2.7-py2.7-win32.egg\reportlab\lib\fonts.py", line 75, in ps2tt 
    raise ValueError("Can't map determine family/bold/italic for %s" % psfn) 
ValueError: Can't map determine family/bold/italic for Own_Font 

預先感謝您。

回答

0

第一個問題是您在registerFontFamily中輸入了錯字。更具體地說,你通過做bold='Own_Font'註冊正常字體爲粗體。

第二個問題是您的字體家族沒有所有類型的字體(在這種情況下是斜體和boldItalic)。根據手冊,如果你看過維拉爲Own_font

如果我們只有維拉常規字體,沒有粗體或斜體那麼我們所有人都必須映射到同一個內部字體名。 <b><i>標籤現在可以安全使用,但不起作用。註冊和映射維拉字體作爲

因此,要解決這個問題後,您應該更換受影響的線路是這樣的:

registerFontFamily('Own_Font',normal='Own_Font',bold='OwnrBold_Font',italic='Own_Font',boldItalic='OwnrBold_Font') 

這應該使<b></b>功能如預期。

+0

這個問題沒有解決這個變化。 :( – Michael

+0

可能指定什麼不工作?你是否有錯誤,是否沒有格式化或是否有其他內容? – B8vrede

+0

請參閱我的編輯!在此先感謝B8vrede! – Michael