Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 ReportLab Library Example to Add Colorful Tables inside PDF Document in Command Line

Posted on January 17, 2023

 

 

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

 

 

Python
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.

 

 

 

Recent Posts

  • Android Java Project to Capture Image From Camera & Save it inside Gallery
  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme