Skip to content

Commit b4fc57d

Browse files
拆分支持多个文件
1 parent 6c8be84 commit b4fc57d

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def update_state():
7878
top = 20;
7979
margin = 36;
8080
labels = ['选择文件', '拆分每页', '部分拆分', '文件合并', '文件剪切', '文件旋转', '添加页码'];
81-
default_values = ['', '支持单个文件', '支持单个文件, 如:[(1, 3), (20, 25), (30, 40)]', '合并后文件名.pdf', '支持单个文件,如:[10, 20, 10, 20, "even", 1] (注:左, 右, 下, 上, odd/even/all, 0/1: 0为全部, 1为测试10张)', '支持单个文件,如:[90, 1] (注:旋转度数是90的整数倍, 0/1: 1为测试一张, 0为全部)', '支持多个文件']
81+
default_values = ['', '支持多个文件', '支持多个文件, 如:[(1, 3), (20, 25), (30, 40)]', '合并后文件名.pdf', '支持单个文件,如:[10, 20, 10, 20, "even", 1] (注:左, 右, 下, 上, odd/even/all, 0/1: 0为全部, 1为测试10张)', '支持单个文件,如:[90, 1] (注:旋转度数是90的整数倍, 0/1: 1为测试一张, 0为全部)', '支持多个文件']
8282
length = len(labels)
8383

8484
for i in range(length):

pdfdo.py

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,34 @@ def pdf_info(self):
1212

1313

1414
def split_pdf_each(self):
15-
try:
16-
pdf_input = PdfFileReader(open(self.infn[0], 'rb'))
17-
pages = pdf_input.getNumPages()
18-
self.message = '正在拆分...'
19-
for i in range(pages):
20-
pdf_output = PdfFileWriter()
21-
pdf_output.addPage(pdf_input.getPage(i))
22-
pdf_output.write(open(self.infn[0][:-4] + '-' + str(i + 1) + '.pdf', 'wb'))
23-
self.message = '已拆分%.2f' %((i + 1)/pages * 100) + '%'
24-
self.message = '拆分完成'
25-
except:
26-
self.message = '出错了,请检查输入格式是否正确'
15+
for infn in self.infn:
16+
try:
17+
pdf_input = PdfFileReader(open(infn, 'rb'))
18+
pages = pdf_input.getNumPages()
19+
self.message = '正在拆分...'
20+
for i in range(pages):
21+
pdf_output = PdfFileWriter()
22+
pdf_output.addPage(pdf_input.getPage(i))
23+
pdf_output.write(open(infn[:-4] + '-' + str(i + 1) + '.pdf', 'wb'))
24+
self.message = '已拆分%.2f' %((i + 1)/pages * 100) + '%'
25+
self.message = '拆分完成'
26+
except:
27+
self.message = '出错了,请检查输入格式是否正确'
2728

2829
def split_pdf_parts(self):
29-
try:
30-
pdf_input = PdfFileReader(open(self.infn[0], 'rb'))
31-
self.message = '正在拆分...'
32-
for part in self.params:
33-
pdf_output = PdfFileWriter()
34-
for i in range(part[0] - 1, part[1]):
35-
pdf_output.addPage(pdf_input.getPage(i));
36-
pdf_output.write(open(self.infn[0][:-4] + '-' + str(part[0]) + '-' + str(part[1]) + '.pdf', 'wb'))
37-
self.message = '第%d部分已拆分'%(self.params.index(part) + 1)
38-
self.message = '拆分完成'
39-
except:
40-
self.message = '出错了,请检查输入格式是否正确'
30+
for infn in self.infn:
31+
try:
32+
pdf_input = PdfFileReader(open(infn, 'rb'))
33+
self.message = '正在拆分...'
34+
for part in self.params:
35+
pdf_output = PdfFileWriter()
36+
for i in range(part[0] - 1, part[1]):
37+
pdf_output.addPage(pdf_input.getPage(i));
38+
pdf_output.write(open(infn[:-4] + '-' + str(part[0]) + '-' + str(part[1]) + '.pdf', 'wb'))
39+
self.message = '第%d部分已拆分'%(self.params.index(part) + 1)
40+
self.message = '拆分完成'
41+
except:
42+
self.message = '出错了,请检查输入格式是否正确'
4143

4244
def merge_pdf(self):
4345
try:
@@ -96,11 +98,11 @@ def rotate_pdf(self):
9698
self.message = '出错了,请检查输入格式是否正确(旋转角度为90的倍数)'
9799

98100
def add_watermark(self):
99-
try:
100-
self.message = '正在添加页码'
101-
water_pdf = PdfFileReader(open('page-number.pdf', 'rb'));
102-
water_pages = water_pdf.getNumPages()
103-
for infn in self.infn:
101+
self.message = '正在添加页码'
102+
water_pdf = PdfFileReader(open('page-number.pdf', 'rb'));
103+
water_pages = water_pdf.getNumPages()
104+
for infn in self.infn:
105+
try:
104106
pdf_input = PdfFileReader(open(infn, 'rb'));
105107
pages = min(pdf_input.getNumPages(), water_pages);
106108
pdf_output = PdfFileWriter();
@@ -111,6 +113,7 @@ def add_watermark(self):
111113
pdf_output.addPage(page);
112114
self.message = str(i) + '/' + str(pages) + ': ' + infn
113115
pdf_output.write(open(infn[:-4] + '-number2.pdf', 'wb'));
114-
self.message = '页码添加完成'
115-
except:
116-
self.message = '出错了,请检查输入格式是否正确(page-number.pdf文件要求和程序在同一目录)'
116+
self.message = '页码添加完成'
117+
except:
118+
self.message = '出错了,请检查输入格式是否正确(page-number.pdf文件要求和程序在同一目录)'
119+

0 commit comments

Comments
 (0)