0

I have a dictionary which I converted to a csv file using pandas dataframe. Below is my csv file:

date,time,type,text,start,end,Year,Movies.type
20/10/15,10:00:00,Horror,"adadadasdsfs
sdsdfsdfasdfsfdf 
   fsdfasdfsfsfsdfs",00:00:00,02:59:13,2015,Horror
20/10/15,11:00:00,Horror,"abc",00:00:00,03:00:00,2015,Horror

I want this file to be converted to PDF using Python. I found that we can use pyfpdf/pdfkit. In what aspects are they different ?

Which module should I use ? How to convert using that module?

keerthi007
  • 223
  • 1
  • 13

1 Answers1

0

I recommend using pypandoc. It can be done like this:

import pypandoc

output = pypandoc.convert_file('somefile.csv', 'pdf', outputfile="somefile.pdf")
assert output == ""
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53
  • Thanks a lot ! Is this is specific to .csv files or is it common to .txt, .json ? What exactly is that assert output=="" mean ? – keerthi007 Jul 12 '20 at 18:38
  • My pleasure. You can do this with all the most common file types, including txt. The final line checks that the conversion was successful, and it is not strictly required. – Gustav Rasmussen Jul 12 '20 at 18:40
  • Here is the official documentation: https://pypi.org/project/pypandoc/ – Gustav Rasmussen Jul 12 '20 at 18:41
  • Thanks mate! But currently getting this error :RuntimeError: Invalid input format! Got "csv" but expected one of these: commonmark, creole, docbook, docx, epub, fb2, gfm, haddock, html, jats, json, l – keerthi007 Jul 12 '20 at 18:41
  • Possibly happening because of _validate_formats in the pypandoc source code: https://github.com/bebraw/pypandoc/blob/ffe82646d2ab6cc4e732960a1e1af6bfcb760bb1/pypandoc/init.py#L202 . I will try to investigate this error further – Gustav Rasmussen Jul 12 '20 at 18:54
  • 1
    Yeah I will also check with that. Thank you so much! – keerthi007 Jul 12 '20 at 18:58