Reportlab - Unicode字符在Unicode支持的字体中显示为框。

时间:2022-09-06 20:16:50

Trying to use reportlab through python 3 to write a document that includes macrons (ā ē ī ō ū), but the macrons are showing up as boxes (■). The document is written in Arial font -- but if I open up the file in a word processor to check the font, the boxes are in 'Segoe UI Symbol' font.

尝试使用reportlab通过python 3写一个文档,包括长音符号(āēīōū),但长音符号显示框(■)。文档是用Arial字体写的——但是如果我在文字处理程序中打开文件检查字体,那么这些方框是“Segoe UI符号”的字体。

For importing in Arial as a font that supports a broad range of unicode characters (which seems to have worked):

以Arial字体作为字体,以支持广泛的unicode字符(似乎已经生效):

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'))

I also import a dictionary through json, looking something like this when I open the json file in notepad:

我还通过json导入了一个字典,当我在记事本中打开json文件时,就像这样。

{"example1":"b\u0101s"}

The program reads and writes this dictionary:

这个程序读和写这本字典:

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)

The outcome should be a pdf with example1:bās but instead comes out as example1:b■s

结果应该是一个pdf格式的,有example1:b s,但结果是作为example1:b s。

2 个解决方案

#1


0  

Find your character under this link:

在这个链接下找到你的角色:

UTF-8 encoding table and Unicode characters

UTF-8编码表和Unicode字符。

  1. Go to (utf-in literal) row of the table.

    转到表的(utf-in literal)行。

  2. You will see some: \xc3\x85 like characters. Choose your character...

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

  3. Then for text output, in your code type something like :

    然后对于文本输出,在您的代码类型中:

    Canvas.drawString(x,y,'\xc3\x85') => and it will print Å ...

    drawstring (x,y,'\xc3\x85') =>,它将打印一个…

So you have to change your dictionary items to UTF-8 LITERALS because it will not understand "b\u0101s"Unicode, which there are many ways to do so ...

因此,您必须将字典条目更改为UTF-8文字,因为它不理解“b\u0101s”Unicode,这有许多方法可以做到这一点……

best regards

致以最亲切的问候

#2


0  

#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"])

#1


0  

Find your character under this link:

在这个链接下找到你的角色:

UTF-8 encoding table and Unicode characters

UTF-8编码表和Unicode字符。

  1. Go to (utf-in literal) row of the table.

    转到表的(utf-in literal)行。

  2. You will see some: \xc3\x85 like characters. Choose your character...

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

  3. Then for text output, in your code type something like :

    然后对于文本输出,在您的代码类型中:

    Canvas.drawString(x,y,'\xc3\x85') => and it will print Å ...

    drawstring (x,y,'\xc3\x85') =>,它将打印一个…

So you have to change your dictionary items to UTF-8 LITERALS because it will not understand "b\u0101s"Unicode, which there are many ways to do so ...

因此,您必须将字典条目更改为UTF-8文字,因为它不理解“b\u0101s”Unicode,这有许多方法可以做到这一点……

best regards

致以最亲切的问候

#2


0  

#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"])