File tree Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Expand file tree Collapse file tree 3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ import os
2
+ import img2pdf
3
+ from pdf2image import convert_from_path
4
+
5
+ # *.jpg to output_filename.pdf convertor
6
+ with open ("output_filename.pdf" , "wb" ) as f :
7
+ f .write (img2pdf .convert ([i for i in os .listdir ("." ) if i .endswith (".jpg" )]))
8
+
9
+ # file.pdf to output_images_folder_name/page_no.jpg convertor
10
+ pages = convert_from_path ("input_filename.pdf" , 500 )
11
+ page_no = 0
12
+ for page in pages :
13
+ # output_images_folder_name = folder needs to be created manually to store all images
14
+ pages [page_no ].save (
15
+ "output_images_folder_name/output_page_{}.jpg" .format (page_no + 1 ), "JPEG"
16
+ )
17
+ page_no += 1
Original file line number Diff line number Diff line change
1
+ # PDF Generator
2
+
3
+ A simple Python Script to generate PDF using Images and vice-versa.
4
+
5
+ # Usage
6
+
7
+ - Create a virtual environment ` $ python3 -m venv /path/to/new/virtual/env `
8
+ - Install required packages using ` $ pip install -r requirements.txt ` for Python2 and ` $ pip3 install -r requirements.txt ` for Python3
9
+
10
+ # Script Explanation
11
+ ## * .jpg to output_filename.pdf convertor
12
+
13
+ - Change ` output_filename.pdf ` with the name of the output pdf file you want
14
+ - ` os.listdir('.') ` points to the present working directory, change it to ` os.listdir('the/directory/path/where/you/want/to/get/images/from') `
15
+ - Change ` i.endswith(".jpg") ` to your desired input image format
16
+
17
+ ## file.pdf to output_images_folder_name/page_no.jpg convertor
18
+
19
+ - Change ` input_filename.pdf ` to the name of the file you want to convert to images
20
+ - Change ` output_images_folder_name/output_page_{}.jpg ` to ` folder_name_that_stores_the_output_images/output_image_name_{}.jpg `
21
+ - ` {} ` ** should not be removed** if you want to store the page no. with the output image name so that you don't loose track of different pages
Original file line number Diff line number Diff line change
1
+ img2pdf >= 0.3.6
2
+ pdf2image >= 1.13.1
3
+ Pillow >= 7.2.0
You can’t perform that action at this time.
0 commit comments