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
預先感謝您。
這個問題沒有解決這個變化。 :( – Michael
可能指定什麼不工作?你是否有錯誤,是否沒有格式化或是否有其他內容? – B8vrede
請參閱我的編輯!在此先感謝B8vrede! – Michael