尝试通过python 3使用reportlab编写包含macrons(āēīōū)的文档,但是macrons显示为框(■)。该文档是用Arial字体编写的 - 但是如果我在文字处理器中打开文件来检查字体,那么这些框就是'Segoe UI Symbol'字体。
用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'))
我还通过json导入字典,当我在记事本中打开json文件时看起来像这样:
{"example1":"b\u0101s"}
程序读写这个词典:
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:b■s
答案 0 :(得分:0)
在此链接下找到您的角色:
UTF-8 encoding table and Unicode characters
转到表格的(utf-in literal)行。
您会看到一些:\xc3\x85
个字符。选择你的角色......
然后对于文本输出,在您的代码中键入类似:
Canvas.drawString(x,y,'\xc3\x85')
=>它将打印Å
...
所以你必须将你的字典项改为UTF-8 LITERALS,因为它不会理解"b\u0101s"
Unicode,有很多方法可以这样做......
最好的问候
答案 1 :(得分: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"])