如果输出使用没有拉丁 unicode 的不同语言,如何在 python 中使用不同的 unicode?

How to use different unicode in python if the output is in different languages that does not have latin unicode?

我正在使用 tkinter 为语言翻译应用程序编写代码,作为一项额外功能,我决定添加一个功能,让您可以将翻译后的文本保存为 pdf 文件。问题是,如果翻译文本包含拉丁 unicode 之外的字符,我会出错。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\vinit\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:/Users/vinit/PycharmProjects/LanguageTranslator/Translator/Translator.py", line 66, in savepdf
    pdf.output("Language.pdf")
  File "C:\Users\vinit\PycharmProjects\LanguageTranslator\venv\lib\site-packages\fpdf\fpdf.py", line 1065, in output
    self.close()
  File "C:\Users\vinit\PycharmProjects\LanguageTranslator\venv\lib\site-packages\fpdf\fpdf.py", line 246, in close
    self._enddoc()
  File "C:\Users\vinit\PycharmProjects\LanguageTranslator\venv\lib\site-packages\fpdf\fpdf.py", line 1636, in _enddoc
    self._putpages()
  File "C:\Users\vinit\PycharmProjects\LanguageTranslator\venv\lib\site-packages\fpdf\fpdf.py", line 1170, in _putpages
    p = self.pages[n].encode("latin1") if PY3K else self.pages[n]
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 87-91: ordinal not in range(256)

如果我想将文本从英语翻译成印地语或任何其他具有不同字符的语言,我该怎么做?

应使用.add_font()设置和使用unicode字体。

下面是使用arialuni.ttf的示例代码:

...
pdf.add_font("arialuni", "", "C:/Windows/fonts/arialuni.ttf", uni=True)
pdf.set_font("arialuni", size=15)
...

详情可参考document