Skip to content

Commit

Permalink
拆分支持多个文件
Browse files Browse the repository at this point in the history
  • Loading branch information
jiandandaoxingfu authored Jan 18, 2021
1 parent 6c8be84 commit b4fc57d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def update_state():
top = 20;
margin = 36;
labels = ['选择文件', '拆分每页', '部分拆分', '文件合并', '文件剪切', '文件旋转', '添加页码'];
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为全部)', '支持多个文件']
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为全部)', '支持多个文件']
length = len(labels)

for i in range(length):
Expand Down
67 changes: 35 additions & 32 deletions pdfdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,34 @@ def pdf_info(self):


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

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

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

def add_watermark(self):
try:
self.message = '正在添加页码'
water_pdf = PdfFileReader(open('page-number.pdf', 'rb'));
water_pages = water_pdf.getNumPages()
for infn in self.infn:
self.message = '正在添加页码'
water_pdf = PdfFileReader(open('page-number.pdf', 'rb'));
water_pages = water_pdf.getNumPages()
for infn in self.infn:
try:
pdf_input = PdfFileReader(open(infn, 'rb'));
pages = min(pdf_input.getNumPages(), water_pages);
pdf_output = PdfFileWriter();
Expand All @@ -111,6 +113,7 @@ def add_watermark(self):
pdf_output.addPage(page);
self.message = str(i) + '/' + str(pages) + ': ' + infn
pdf_output.write(open(infn[:-4] + '-number2.pdf', 'wb'));
self.message = '页码添加完成'
except:
self.message = '出错了,请检查输入格式是否正确(page-number.pdf文件要求和程序在同一目录)'
self.message = '页码添加完成'
except:
self.message = '出错了,请检查输入格式是否正确(page-number.pdf文件要求和程序在同一目录)'

0 comments on commit b4fc57d

Please sign in to comment.