-
Notifications
You must be signed in to change notification settings - Fork 19
/
PdfMake.py
64 lines (55 loc) · 2.22 KB
/
PdfMake.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""
Created on :2018/04/03
@author: Freeman
"""
from PyPDF2 import PdfFileReader, PdfFileWriter
__all__ = ['PDFMake']
class PDFMake(object):
def __init__(self, outputdirectory=''):
self.outputdirectory = outputdirectory
def __check_page_list(self):
# 检测是否为string类型
if type(self.listmode) != type('0-6,8-9'):
return True
else:
# 检测是否含有非法字符
for i in range(1, len(self.listmode) - 1):
if self.listmode[i] not in \
['-', ',', '\'', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
return True
return False
def spilt_pdf(self, inputfilename=None, listmode=None):
self.listmode = listmode
# 读取PDF文件
pdfFile = PdfFileReader(open(inputfilename, "rb"))
pageCount = pdfFile.getNumPages()
if self.listmode == None:
for i in range(pageCount):
pdfWriter = PdfFileWriter()
page = pdfFile.getPage(i)
pdfWriter.addPage(page)
pdfWriterNmae = self.outputdirectory +'/page{}'.format(i+1) + '.pdf'
pdfWriter.write(open(pdfWriterNmae, 'wb'))
else:
if self.__check_page_list():
raise ValueError
else:
pdfWriter = PdfFileWriter()
part = self.listmode.split(',')
for k in part:
start = int(k.split('-')[0])
end = int(k.split('-')[1])
for m in range(start - 1, end):
page = pdfFile.getPage(m)
pdfWriter.addPage(page)
pdfWriterNmae = self.outputdirectory + '/part ' + self.listmode + '.pdf'
pdfWriter.write(open(pdfWriterNmae, 'wb'))
def merge_pdf(self, flienamelist=None):
print(flienamelist)
pdfWriter = PdfFileWriter()
for i in flienamelist:
pdfFile = PdfFileReader(open(i, "rb"))
pdfWriter.appendPagesFromReader(pdfFile)
pdfWriterNmae = self.outputdirectory + '/mergeByMaster.pdf'
pdfWriter.write(open(pdfWriterNmae, 'wb'))