In this Python tutorial, we will learn how to Create and modify pdf file in Python, and also we will cover these topics:
- How to create a pdf file in python
- Create pdf by taking size A4
- Create pdf by taking size A5
- Create a table in pdf using python
- Create a table with the grid in pdf using python
- Python create pdf from images
- How to rotate the pdf in python
- How to create a pdf of days in a year python
- Python create pdf from HTML
- Python creates a pdf from a text file
- Python create pdf from images with each page having individual image sizes
PDF – Portable Document Format
To create a pdf in Python, we can take different size of pages such as:
- A4
- A3
- A5
- letter
- legal
How to create a pdf file in python
Now, we can see how to create a pdf file in python.
- In this example, I have imported a module called FPDF from fpdf. The fpdf is a library that is used to generate pdf documents in python.
- To add the page, I have taken pdf.add_page() and to set the font pdf.set_font is used. Here, I have used Arial-type font and assigned size = 14.
- The pdf.cell is used to print the cell with an optional border and background.
- The width = 200, height = 10, and txt = "welcome to PythonGuides", and length = 1 and taken left alignment as align="L".
- To generate the pdf file, I have used pdf.output("python.pdf"). The python.pdf is the name of the pdf with the extension.
Example:
from fpdf import FPDF pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=14) pdf.cell(200, 10, txt="Welcome to PythonGuides", ln=1, align="L") pdf.output("python.pdf")
We can see the generated pdf file as the output. You can refer to the below screenshot for the output.
This is how to create a pdf file in Python.
You may like Python concatenate list and Python string formatting with examples.
Create pdf by taking size A4 in Python
Now, we can see how to create pdf by taking size A4 in python.
- In this example, I have imported a module called FPDF from fpdf and declared a variable as pdf and assigned orientation = 'p', where p as portrait and unit is taken to measure and the format as 'A4'.
- The font is taken as "Arial", and size=20 and cell are taken as 200,10, and "Welcome to PythonGuides" is the text to write in the pdf file, the length is assigned as length=1, and alignment is set to left.
- To generate the pdf file, I have used pdf.output("kushi.pdf"). The kushi is the name of the file along with the extension.
Example:
from fpdf import FPDF pdf = FPDF(orientation='P', unit='mm', format='A4') pdf.add_page() pdf.set_font("Arial", size=20) pdf.cell(200, 10, txt="Welcome to PythonGuides", ln=1, align="L") pdf.output("kushi.pdf")
Here, we can see pdf generated with A4 format as the output. You can refer to the below screenshot for the output.
This is how to create pdf by taking size A4 in Python.
Read: PdfFileWriter Python Examples
Create pdf by taking size A5 in Python
Now, we can see how to create pdf by taking size A5 in python.
- In this example, I have imported a module called FPDF from fpdf and declared a variable as pdf and assigned orientation = 'p', where p as portrait and unit is taken in mm and the format as 'A5'.
- The font is taken as "Arial", and size=20 and cell are taken as 200,10, and "Welcome to PythonGuides" is the text to write in the pdf file. and length=1 and alignment is set to left.
- To generate the pdf file, I have used pdf.output("kushi.pdf"). kushi is the name of the file along with the extension.
Example:
from fpdf import FPDF pdf = FPDF(orientation='P', unit='mm', format='A5') pdf.add_page() pdf.set_font("Arial", size=15) pdf.cell(200, 10, txt="Welcome to PythonGuides", ln=1, align="L") pdf.output("kushi.pdf")
Here, we can see pdf generated with A5 format as the output. You can refer to the below screenshot for the output.
This is how to create pdf by taking size A5 in Python.
You may like How to read video frames in Python?
Create a table in pdf using Python
Here, we can see how to Create a table in pdf using python.
- In this example, I have imported modules called colors from reportlab.lib and module A4 from reportlab.lib.pagesizes. and also imported modules like SimpleDocTemplate, Table, TableStyle from reportlab.platypus.
- The pdf.cell is used to print the cell with an optional border and background.
- The variable called document is declared and assigned SimpleDocTemplate("table.pdf", pagesize=A4),table.pdf is the name of the pdf file and page size as A4.
- An empty variable is declared as items and another variable is declared as data that contains some items in it.
- To create a table, I have used t=Table(data) and to insert the items into the table items.append(t) is used.
- The document.build(items) is used to automatically generate a pdf.
Example:
from reportlab.lib import colors from reportlab.lib.pagesizes import A4 from reportlab.platypus import SimpleDocTemplate, Table, TableStyle document = SimpleDocTemplate("table.pdf", pagesize=A4) items = [] data= [['Apple', 'Mango', 'Kiwi'], ['Tomato', 'Potato', 'Peas']] t=Table(data) items.append(t) document.build(items)
The below screenshot shows the generated pdf with table without grid as the output.
This is how to create a table in pdf using python.
Read, Extract text from PDF Python
Create a table with the grid in pdf using python
Here, we can see how to create a table with the grid in pdf using python.
- In this example, I have imported a module called colors from reportlab.lib and letter, inch from reportlab.lib.pagesizes.
- The color module has some predefined basic colors that we can use.
- The module SimpleDocTemplate, Table, TableStyle is imported from reportlab.platypus.
- An empty variable is declared as items and another variable is declared as data that contains some items in it.
- To create a table, I have used t=Table(data) and to insert the items into the table items.append(t) is used.
- The indexing in Reportlab starts at
(0, 0)
for the first row, first column. - So
(1, 1)
only applies the styling to everything below the first row and right of the first column. - To get 2 rows and 5 coloumns, I have used t=Table(data,5*[1*inch], 2*[1*inch]).
- The style of the table is given by alignments such as right and top and also used VALIGN.
- The INNERGRID is used to split the table.
- The items.append(t) is used to append the data into the table.
- The document.build(items) is used to automatically generate a pdf.
Example:
from reportlab.lib import colors from reportlab.lib.pagesizes import letter, inch from reportlab.platypus import SimpleDocTemplate, Table, TableStyle document = SimpleDocTemplate("table.pdf", pagesize=letter) items = [] data= [['0', '2', '4', '6', '8'], ['10', '12', '14', '16', '18']] t=Table(data,5*[1*inch], 2*[1*inch]) t.setStyle(TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'), ('VALIGN',(-1,-1),(-1,-1),'RIGHT'), ('ALIGN',(-1,-1),(-1,-1),'RIGHT'), ('VALIGN',(-1,-1),(-1,-1),'TOP'), ('INNERGRID', (0,0), (-1,-1), 1, colors.black), ('BOX', (0,0), (-1,-1), 0.25, colors.black),])) items.append(t) document.build(items)
The below screenshot shows the output :
This is how to create a table with the grid in pdf using python.
Python create pdf from images
Here, we can see how to create pdf from images in python.
- In this example, I have imported a module called img2pdf and module Image from PIL and also OS.
- The variable is declared as imagepath and assigned the path of the image.
- Here cartoon.pdf is the name of the pdf to be created.
- To write the imagefile in the pdf f.write is used and then to close the file file.close() is used.
Example:
import img2pdf from PIL import Image import os imagepath = r"C:\Users\Administrator.SHAREPOINTSKY\Downloads\mickey.png" pdfname = "cartoon.pdf" image = Image.open(imagepath) pdf_bytes = img2pdf.convert(image.filename) file = open(pdfname, "wb") file.write(pdf_bytes) image.close() file.close()
We can that the imagefile is in pdf format as the output. You can refer to the below screenshot for the output:
This is how to create pdf from images in Python.
How to rotate a pdf in Python
Now, we can see how to rotate pdf in python.
- In this example, I have imported a module called pikepdf and to select the pdf, I have used pdf = pikepdf.Pdf.open('cartoon.pdf').
- The cartoon.pdf is the name of the file for loop is used to rotate the pdf, I have used the page.Rotate = 180.
- To save the new rotated pdf with another name to that file, I have used pdf.save('rotated.pdf') the rotated.pdf is the name of the new file.
Example:
import pikepdf pdf = pikepdf.Pdf.open('cartoon.pdf') for page in pdf.pages: page.Rotate = 180 pdf.save('rotated.pdf')
The below screenshot shows the output:
This is how to rotate a pdf in Python.
How to create a pdf of days in a year python
Now, we can see How to create a pdf of days in a year in python.
- In this example, I have imported a module called inch from reportlablib, colors from reportlab.lib, and imported A4 from reportlab.lib.pagesizes.
- The module SimpleDocTemplate, Table, TableStyle is imported from reportlab.platypus.
- The module Drawing is imported from reportlab.graphics.shapes.
- To get the calendar in the pdf, I have imported a module called a calendar.
- An empty variable is declared as items and another variable is declared as data that contains some items in it.
- To assign the days of the week, I have used cal = [['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']].
- The .extend is used to add the elements to the table.
- To divide the rows and coloumns of the table, I have used table = Table(cal, 7*[inch], len(cal) * [inch]).
- I have used Helvetica font and alignment is taken according to the requirement and inner grid is colored with blue and the box is colored with the color red.
- The document.build(items) is used to automatically generate a pdf.
Example:
from reportlab.lib.units import inch from reportlab.lib import colors from reportlab.lib.pagesizes import A4 from reportlab.platypus import SimpleDocTemplate, Table, TableStyle from reportlab.graphics.shapes import Drawing import calendar doc = SimpleDocTemplate('cal.pdf', pagesize=A4) items = [] cal = [['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']] cal.extend(calendar.monthcalendar(2021,2)) table = Table(cal, 7*[inch], len(cal) * [inch]) table.setStyle(TableStyle([ ('FONT', (0, 0), (-1, -1), 'Helvetica'), ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'), ('FONTSIZE', (0, 0), (-1, -1), 8), ('INNERGRID', (0, 0), (-1, -1), 0.5, colors.blue), ('BOX', (0, 0), (-1, -1), 0.5, colors.red), ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), ])) items.append(table) doc.build([table])
The below screenshot shows the calender in the pdf as the output.
This is how to create a pdf of days in a year in Python.
Python create pdf from HTML
- Firstly, we have to install pdfkit by using pip install pdfkit.
- We have to download wkhtmltopdf by using the link: WKHTMLTOPDF
- After downloading we have to copy the path of wkhtmltopdf file and paste it into the environmental variable.
Here, we can see how to create pdf from HTML in python.
- In this example, I have imported a module called pdfkit.
- The path of the file is assigned along with .exe. The variable config is defined and pdfkit.configuration() is assigned, this takes the configuration options as the initial parameter
- To convert the html file to pdf, I have used pdfkit.from_file('kavita.html', 'newfile.pdf', configuration=config.
- The kavita.html is the name of the Html file and newfile is the generated pdf file.
Example:
import pdfkit path_wkhtmltopdf = r'C:/Users/Administrator.SHAREPOINTSKY/Desktop/Work/wkhtmltopdf/bin/wkhtmltopdf.exe' config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) pdfkit.from_file('kavita.html', 'newfile.pdf', configuration=config)
We can see the generated new pdf as the output. You can refer to the below screenshot for the output.
This is how we can create a pdf from HTML in Python.
Python create a pdf from a text file
Here, we cam see how to create a pdf from text file in python.
- In this example, I have imported a module called FPDP from fpdf. This is a library used to generate pdf documents.
- To add the page, I have used pdf.add_page() to set the font "Arial" and size = 15 is assigned.
- To open the file, I have assigned the path of the file and "r" used to read the file.
- The for loop is used, the size of the cell is given as pdf.cell(200, 10, txt = x, ln = 5, align = 'L') and alignment as left.
- To get the generated pdf, I have used pdf.output("textfile.pdf").
Example:
from fpdf import FPDF pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size = 15) f = open(r"C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\my_file.txt", "r") for x in f: pdf.cell(200, 10, txt = x, ln = 5, align = 'L') pdf.output("textfile.pdf")
The below screenshot shows the generated pdf as the output:
This is how we can create a pdf from a text file in Python.
Python create pdf from images with each page having individual image sizes
Here, we can see how to create pdf from images with each page having individual image sizes in python.
- Here, I have imported a module called Image from PIL and also imported img2pdf and to open the file, I have used with open( 'cartoonsize.pdf', 'wb' ) as f, and 'wb' mode is used to write the file in binary mode.
- The size of the page is assigned as an image.width, 50 and image.height,50. The FitMode is used to fill the exact shrink enlarge.
- To write the image into the pdf f.write is used to convert the image to pdf and the path of the file is assigned.
- We can see the number of pages in the corner of the pdf.
Example:
from PIL import Image import img2pdf with open( 'cartoonsize.pdf', 'wb' ) as f: image = Image.open( r'C:\Users\Administrator.SHAREPOINTSKY\doll.jpg' ) my_layout_fun = img2pdf.get_layout_fun( pagesize = ( img2pdf.px_to_pt( image.width, 50 ), img2pdf.px_to_pt( image.height, 50) ), fit = img2pdf.FitMode.into ) f.write( img2pdf.convert( [r'C:\Users\Administrator.SHAREPOINTSKY\doll.jpg', r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\cat.jpg'], layout_fun = my_layout_fun ))
Here, we can see two pages of pdf that contain images with sizes assigned to them as the output. You can refer to the below screenshot for the output:
You may like the following Python tutorials:
- Create a game using Python Pygame (Tic tac toe game)
- Python Pandas CSV Tutorial
- How to Convert DateTime to UNIX timestamp in Python
- Python catch multiple exceptions
- Python Pygame tutorial
- Python String Functions
- Python naming conventions
- Python intersection of sets
In this tutorial, we have learned about Create and modify pdf file in python, and also we have covered these topics:
- How to create a pdf file in python
- Create pdf by taking size A4
- Create pdf by taking size A5
- Create a table in pdf using python
- Create a table with the grid in pdf using python
- Python create pdf from images
- How to rotate the pdf in python
- How to create a pdf of days in a year python
- Python create pdf from HTML
- Python creates a pdf from a text file
- Python create pdf from images with each page having individual image sizes
Source: https://pythonguides.com/create-and-modify-pdf-file-in-python/
Posted by: rubinpaasche0209521.blogspot.com
0 Comments