reportlab 使用 registerFont 添加字体

Mic*_*ael 5 python pdf fonts reportlab

我必须使用reportlab.pdfbase.pdfmetrics 中的registerFontFamily 方法。我尝试向系列中添加两种字体,但无法将粗体字体与“< b>sometext </b>”一起使用。我的报告需要此功能。我只能用普通的,不知道为什么。

这是代码。

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) )
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

  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
Run Code Online (Sandbox Code Playgroud)

先感谢您。

B8v*_*ede 2

第一个问题是您在registerFontFamily. 更具体地说,您可以通过以下操作将普通字体注册为粗体bold='Own_Font'

第二个问题是您的字体系列没有适用于所有类型的字体(在本例中为斜体和粗体斜体)。根据手册,如果您将 Vera 读为Own_font

如果我们只有 Vera 常规字体,没有粗体或斜体,那么我们必须将所有字体映射到相同的内部字体名称。<b><i>标签现在可以安全使用,但没有任何效果。将 Vera 字体注册并映射为

因此,要修复它,您应该像这样替换受影响的行:

registerFontFamily('Own_Font',normal='Own_Font',bold='OwnrBold_Font',italic='Own_Font',boldItalic='OwnrBold_Font')
Run Code Online (Sandbox Code Playgroud)

这应该可以<b></b>按预期发挥作用。