How to write a table from CSV to PDF using Python

12,175

i really like using pdfkit for these things. setup is dead simple, just do pip install pdfkit and install wkhtmltopdf on your system.

and then from a python-commandline of your liking, do:

import pdfkit
pdfkit.from_file('/path/to/your/testcsv.csv',
                 '/path/of/output/testcsv.pdf')

more on this here

For windows (or if your wkhtmltopdf is not in $PATH), you can provide an optional configuration-parameter:

config = pdfkit.configuration(wkhtmltopdf='/path/to/wkhtmltopdf')
pdfkit.from_file('/path/to/your/testcsv.csv',
                 '/path/of/output/testcsv.pdf',
                 configuration=config
)

more on this here in 'Configuration'-Section.

Share:
12,175

Related videos on Youtube

Riccardo
Author by

Riccardo

Updated on June 04, 2022

Comments

  • Riccardo
    Riccardo almost 2 years

    I have a CSV file which contains the following table:

    users        passwords        company
    Admin        test_psw         test_cmp
    test_user1   test_psw1        test_cmp1
    ...          ...              ...
    ...          ...              ...
    

    where users, passwords and company are the header of the table and the table itself can dynamically grow up.

    How can I write this kind of table to a PDF file using python? I had a look on reportlab library, but came out with no ideas.

    • joppich
      joppich about 6 years
      do you need to do any data processing between reading the csv and writing to pdf?
    • Riccardo
      Riccardo about 6 years
      @joppich No, bu the PDF must be readable and upgradable.
  • Riccardo
    Riccardo about 6 years
    it gives me the following error OSError: No wkhtmltopdf executable found: "b''" If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf Evend after installing wkhtmltopdf
  • joppich
    joppich about 6 years
    sorry, updated my answer to point out that dependency. what os are you on?
  • Riccardo
    Riccardo about 6 years
    I'm on Windows 10
  • joppich
    joppich about 6 years
    provided some additional info, hope it helps. i don't have windows, thus untested
  • Riccardo
    Riccardo about 6 years
    thanks man, but it gives me the following error: Loading pages (1/6) Error: Failed loading page file:///C:/Users/r.spampinato/PycharmProjects/test/reports/1‌​.1-IAM.csv (sometimes it will work just to ignore this error with --load-error-handling ignore) Exit with code 1, due to unknown error.
  • joppich
    joppich about 6 years
    that --load-error-handling ignore-thing is remarkably unhelpful, wow... sorry, i'm at a loss here. good luck, though.
  • Miffy
    Miffy about 5 years
    OSError: No wkhtmltopdf executable found: "b''" If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf Evend after installing wkhtmltopdf. Im also getting same error. And Im using Mac OS. Please help