SOURCE CODE
First of all guys you need to install the below library using the pip
command as shown below
pip install reportlab
After that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from reportlab.lib import colors from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Table, TableStyle doc = SimpleDocTemplate("simple_table.pdf", pagesize=letter) # container for the 'Flowable' objects elements = [] data = [['Name', 'Age', 'Country'], ['Williamson', '11', 'New Zealand'], ['Stuart', '21', 'New Zealand'], ['Kane', '31', 'New Zealand']] t = Table(data) t.setStyle(TableStyle([('BACKGROUND', (1, 1), (-2, -2), colors.black), ('TEXTCOLOR', (0, 0), (1, -1), colors.red)])) elements.append(t) # write the document to disk doc.build(elements) |
As you can see we are importing the reportlab
library and then we are defining the columns
and rows
and then we are making the Table
in the pdf document passing the data. And then we are setting the styles
of the table using the setStyles()
method here we are changing the background color
and the text color
of the rows and columns. And then we are saving the pdf
document with the custom filename.