【问题标题】:Pyfpdf Turkish Character in PythonPython中的Pyfpdf土耳其字符
【发布时间】:2020-02-05 19:52:31
【问题描述】:

我想在我的 Python 应用程序中使用包含土耳其语字符的文本创建一个 pdf,但我收到一个错误。我的代码如下。我该如何解决这个问题?

# -*- coding: utf-8 -*-
from fpdf import FPDF
import os

def add_image(image_path):
    pdf = FPDF()
    pdf.add_page()

    epw = pdf.w - 2 * pdf.l_margin
    pdf.set_font('Arial', 'B', 14.0)
    txt = u'ATATÜRK LİSESİ 2019 2020 EĞİTİM ÖĞRETİM YILI 11C SINIFI'
    stxt = txt.encode('iso-8859-9')
    pdf.cell(epw, 0.0, stxt, align='C')

如果我使用下面的代码,我会收到一个 'UnicodeEncodeError: 'latin-1' codec can't encode character '\u0130' in position 60: ordinal not in range(256)' 错误

epw = pdf.w - 2 * pdf.l_margin
pdf.set_font('Arial', 'B', 14.0)
txt = 'ATATÜRK LİSESİ 2019 2020 EĞİTİM ÖĞRETİM YILI 11C SINIFI'
#stxt = txt.encode('iso-8859-9')
pdf.cell(epw, 0.0, txt, align='C')

【问题讨论】:

  • 你得到什么错误?
  • TypeError: a bytes-like object is required, not 'str'
  • 仅供参考:该错误消息是正确的。 Latin-1 编码不包含上面带有点的大写拉丁字母 i。也许尝试另一种编码。

标签: python pyfpdf


【解决方案1】:

我将“tr-arial.ttf”字体下载到应用程序文件夹中,我找到了这个解决方案:

epw = pdf.w - 2 * pdf.l_margin
txt = u'ATATÜRK LİSESİ 2019 2020 EĞİTİM ÖĞRETİM YILI 11C SINIFI'
pdf.add_font('tr-arial', '', 'tr-arial.ttf', uni=True)
pdf.set_font('tr-arial', '', 11)
pdf.cell(epw, 0.0, txt, align='C')

【讨论】:

    猜你喜欢
    • 2014-07-25
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多