Python's fpdf responds with "RuntimeError: TTF Font file not found: C:\Windows\Fonts\DejaVuSansCondensed.ttf" despite font being installed

Python's fpdf responds with "RuntimeError: TTF Font file not found: C:\Windows\Fonts\DejaVuSansCondensed.ttf" despite font being installed

我有一个带有 utf-8 字符的 csv,我想使用 Python 的 fpdf 包将其转换为 pdf。

当我执行以下代码时,收到错误,RuntimeError: TTF Font file not found: C:\Windows\Fonts\DejaVuSansCondensed.ttf

这是完整的堆栈跟踪:

Traceback (most recent call last):
  File "C:/Users/abcd/PycharmProjects/bronze/pdfParser.py", line 40, in <module>
    pdf.add_font('DejaVu', '', "C:\Windows\Fonts\DejaVuSansCondensed.ttf", uni=True)
  File "C:\Users\abcd\PycharmProjects\bronze\venv\lib\site-packages\fpdf\fpdf.py", line 469, in add_font
    raise RuntimeError("TTF Font file not found: %s" % fname)
RuntimeError: TTF Font file not found: C:\Windows\Fonts\DejaVuSansCondensed.ttf

以下是我的代码的相关部分:

from fpdf import FPDF

lines = []

with open("intake.csv", "r") as f:
    data = f.readlines()
for line in data:
    lines.append(line[1:-1].split('","'))

pdf = FPDF()
pdf.add_page()

# This following line SHOULD work, but does not
pdf.add_font('DejaVu', '', "C:\Windows\Fonts\DejaVuSansCondensed.ttf", uni=True)
pdf.set_font('DejaVu', '', 14)


for entry in range(0, len(lines[0])):
    question = str(entry) + ") " + lines[0][entry]
    pdf.cell(40, 10, question, ln=1)
    pdf.cell(40, 10, lines[1][entry], ln=1)

    # make a new line
    pdf.cell(40, 10, "", ln=1)
    pdf.cell(40, 10, "", ln=1)

pdf.output("tuto-test.pdf", "F")

当我 运行 这样做时,我应该得到一个不错的小 pdf。相反,它对我大喊我没有安装字体。

我的电脑上确实安装了这种字体。我已经直接安装了字体并且从我的 OS 收到了一条消息,基本上是“字体已经安装”。我还重新启动了我的电脑和我的 PyCharm 程序。

我找到了一个与我的问题相关的 Google 群组帖子,但我无法弄清楚 request.folder 是什么(不是请求,“请求”,不是 s)。 https://groups.google.com/g/web2py/c/UZf7qJ6KDa8

这似乎是一个非常小众的问题,所以如果有人能解决我的问题,我将印象深刻。谢谢

闻起来像 windows 权限问题,根据 official docs:

os.path.exists(path) Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

因此您应该授予自己访问该文件夹的权限(不推荐),或者将字体复制到您拥有的目录中。