[英]Reportlab - Unicode characters appear as boxes in unicode-supported font
Trying to use reportlab through python 3 to write a document that includes macrons (ā ē ī ō ū), but the macrons are showing up as boxes (■). 试图通过python 3使用reportlab编写一个包含宏子(āēīōū)的文档,但是这些宏子显示为方框(■)。 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. 该文档以Arial字体书写-但是,如果我在文字处理器中打开文件以检查字体,则这些框将以“ Segoe UI Symbol”字体显示。
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
结果应为带有example1:bās
的pdf,但应作为example1:b■s
Find your character under this link: 在此链接下找到您的角色:
UTF-8 encoding table and Unicode characters UTF-8编码表和Unicode字符
Go to (utf-in literal) row of the table. 转到表的(utf-in文字)行。
You will see some: \\xc3\\x85
like characters. 您会看到一些: \\xc3\\x85
类的字符。 Choose your character... 选择你的角色...
Then for text output, in your code type something like : 然后,对于文本输出,在您的代码中输入以下内容:
Canvas.drawString(x,y,'\\xc3\\x85')
=> and it will print Å
... Canvas.drawString(x,y,'\\xc3\\x85')
=>它将打印Å
...
So you have to change your dictionary items to UTF-8 LITERALS because it will not understand "b\ās"
Unicode, which there are many ways to do so ... 因此,您必须将字典项更改为UTF-8 LITERALS,因为它将无法理解"b\ās"
Unicode,这有很多方法可以实现...
best regards 最好的祝福
#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"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.