python png to pdf code example

Example 1: python pdf to image

#The pdf2image library can be used
#You can install it simply using,

pip install pdf2image
#Once installed you can use following code to get images.

from pdf2image import convert_from_path
pages = convert_from_path('pdf_file', 500)

#Saving pages in jpeg format

for page in pages:
    page.save('out.jpg', 'JPEG')

Example 2: pdf to text python

#!pip install tabula-py
import tabula
#read all table data
df = tabula.read_pdf("sample.pdf",pages=[1,2])
df[1]

#tabula.convert_into("sample.pdf", "sample.csv", output_format="csv")

Example 3: pil img to pdf

from PIL import Image

image1 = Image.open(r'path where the image is stored\file name.png')
im1 = image1.convert('RGB')
im1.save(r'path where the pdf will be stored\new file name.pdf')

Example 4: pil img to pdf

from PIL import Image

image1 = Image.open(r'path where the image is stored\file name.png')
im1 = image1.convert('RGB')
im1.save(r'path where the pdf will be stored\new file name.pdf')

Tags:

Misc Example