-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_converter.py
More file actions
46 lines (34 loc) · 1.3 KB
/
pdf_converter.py
File metadata and controls
46 lines (34 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from pdf2image import convert_from_path
from PyPDF2 import PdfReader
import os
pdf_doc = "/Users/jacob/Downloads/scene_walden/Programme_colline.pdf"
dest_path = "/Users/jacob/Downloads/scene_walden/program"
def parseDocument(path):
"""Derrive information from a pdf file."""
pdfFile = PdfReader(path)
maxWid = pdfFile.pages[0].mediabox.width
maxHeight = pdfFile.pages[0].mediabox.height
for page in pdfFile.pages:
if page.mediabox.width > maxWid:
maxWid = page.mediabox.width
if page.mediabox.height > maxHeight:
maxHeight = page.mediabox.height
return {
"dimensions" : [int(maxWid), int(maxHeight)],
"pages" : len(pdfFile.pages)
}
def convertPDF(pathIn, pathOut, numPages, createFile):
if createFile:
images = convert_from_path(pathIn)
filename = os.path.splitext(os.path.basename(pathIn))[0]
fileList = []
for i in range(numPages):
if createFile:
images[i].save(os.path.join(pathOut, filename + "_page_" + str(i + 1) +'.jpg'), 'JPEG')
fileList.append(filename + "_page_" + str(i + 1) +'.jpg')
return fileList
doc_info = parseDocument(pdf_doc)
print(doc_info)
convertPDF(pdf_doc, dest_path, doc_info["pages"], True)
#convertPDF(pdf_doc, dest_path, pages, True)
print("done.")