关于python:Reportlab-Unicode字符以Unicode支持的字体显示为方框

Reportlab - Unicode characters appear as boxes in unicode-supported font

试图通过python 3使用reportlab编写一个包含宏子(āēīōū)的文档,但是这些宏子显示为方框()。该文档以Arial字体编写-但是如果我在文字处理器中打开文件以检查字体,则这些框将使用\\'Segoe UI Symbol \\'字体。

用于以支持多种unicode字符(似乎有效)的字体导入Arial:

1
2
3
4
5
import reportlab.rl_config
reportlab.rl_config.warnOnMissingFontGlyphs = 0
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont('Arial', 'Arial.ttf'))

我也通过json导入字典,当我在记事本中打开json文件时,外观如下所示:

1
{"example1":"b\\u0101s"}

程序读取和写入此字典:

1
2
3
4
5
6
7
8
9
10
11
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
doc = SimpleDocTemplate("hello.pdf")
Story = [Spacer(1,2*inch)]
style = styles["Normal"]
with open('CompDict.json','r') as f:
        m_dic=json.load(f)
for key,value in m_dic:
     p=Paragraph(key+":"+value,style)
     Story.append(p)
doc.build(Story)

结果应为带有example1:bās的pdf,但应显示为example1:bs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#Here I am writing chunks of code, hope you will understand

from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer  
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle  
from reportlab.lib.enums import TA_CENTER,TA_JUSTIFY      
from reportlab.pdfbase import pdfmetrics      
from reportlab.pdfbase.ttfonts import TTFont  
from reportlab.lib.fonts import addMapping

pdfmetrics.registerFont(TTFont('devaNagri', 'NotoSerifDevanagari.ttf')) # devaNagri is a folder located in **/usr/share/fonts/truetype** and 'NotoSerifDevanagari.ttf' file you just download it from https://www.google.com/get/noto/#sans-deva and move to devaNagri folder.

addMapping('devaNagri', 0, 0, 'NotoSerifDevanagari') #devnagri is a folder name and NotoSerifDevanagari is file name

style = getSampleStyleSheet()  
style.add(ParagraphStyle(name="ParagraphTitle", alignment=TA_JUSTIFY, fontName="devaNagri")) # after mapping fontName define your folder name.  
paragraph = Paragraph('your unicode string', style["ParagraphTitle"])

在此链接下找到您的角色:

UTF-8编码表和Unicode字符

  • 转到表的(utf-in文字)。

  • 您将看到一些:\\xc3\\x85字符。选择你的角色...

  • 然后用于文本输出,在您的代码中输入以下内容:

    Canvas.drawString(x,y,'\\xc3\\x85') =>它将打印? ...

  • 因此您必须将字典项更改为UTF-8 LITERALS,因为它将无法理解"b\\u0101s" Unicode,这有很多方法可以实现...

    最诚挚的问候