Export Pandas DataFrame into a PDF file using Python Export Pandas DataFrame into a PDF file using Python python python

Export Pandas DataFrame into a PDF file using Python


First plot table with matplotlib then generate pdf

import pandas as pdimport numpy as npimport matplotlib.pyplot as pltfrom matplotlib.backends.backend_pdf import PdfPagesdf = pd.DataFrame(np.random.random((10,3)), columns = ("col 1", "col 2", "col 3"))#https://stackoverflow.com/questions/32137396/how-do-i-plot-only-a-table-in-matplotlibfig, ax =plt.subplots(figsize=(12,4))ax.axis('tight')ax.axis('off')the_table = ax.table(cellText=df.values,colLabels=df.columns,loc='center')#https://stackoverflow.com/questions/4042192/reduce-left-and-right-margins-in-matplotlib-plotpp = PdfPages("foo.pdf")pp.savefig(fig, bbox_inches='tight')pp.close()

reference:

How do I plot only a table in Matplotlib?

Reduce left and right margins in matplotlib plot


Here is how I do it from sqlite database using sqlite3, pandas and pdfkit

import pandas as pdimport pdfkit as pdfimport sqlite3con=sqlite3.connect("baza.db")df=pd.read_sql_query("select * from dobit", con)df.to_html('/home/linux/izvestaj.html')nazivFajla='/home/linux/pdfPrintOut.pdf'pdf.from_file('/home/linux/izvestaj.html', nazivFajla)


Well one way is to use markdown. You can use df.to_html(). This converts the dataframe into a html table. From there you can put the generated html into a markdown file (.md) (see http://daringfireball.net/projects/markdown/basics). From there, there are utilities to convert markdown into a pdf (https://www.npmjs.com/package/markdown-pdf).

One all-in-one tool for this method is to use Atom text editor (https://atom.io/). There you can use an extension, search "markdown to pdf", which will make the conversion for you.

Note: When using to_html() recently I had to remove extra '\n' characters for some reason. I chose to use Atom -> Find -> '\n' -> Replace "".

Overall this should do the trick!