[Python] Python's fpdf responds with "RuntimeError: TTF Font file not found:...

Discussão em 'Python' iniciado por Stack, Março 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have a csv with utf-8 characters that I want to convert to a pdf using Python's fpdf package.

    When I execute the following code, I receive the error, RuntimeError: TTF Font file not found: C:\Windows\Fonts\DejaVuSansCondensed.ttf

    Here is the full stack trace:

    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


    Here are the relevant parts of my code:

    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")



    When I run this I should get a nice little pdf. Instead it yells at me that I do not have a font installed.

    I absolutely do have this font installed on my PC. I've installed the font directly and received a message from my OS, "Font is already installed" basically. I have also restarted both my PC and my PyCharm program.

    I found a Google Groups thread related to my problem but I couldn't figure out what the request.folder thing is (NOT requests, "request", no s). https://groups.google.com/g/web2py/c/UZf7qJ6KDa8

    Seems like a really niche problem so if anyone can solve my problem I'll be really impressed. Thanks

    Continue reading...

Compartilhe esta Página