Skip to content

Commit d6e2649

Browse files
add word2pdf in gui
1 parent 9825dd1 commit d6e2649

File tree

13 files changed

+203
-298
lines changed

13 files changed

+203
-298
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pids
1414
*.pid
1515
*.seed
1616
*.pid.lock
17+
*.spec
1718

1819
# Directory for instrumented libs generated by jscoverage/JSCover
1920
lib-cov

A4.pdf

-1.17 KB
Binary file not shown.

PDF.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from PyPDF2 import PdfFileReader, PdfFileWriter
22
import sys
33
import os
4-
import fitz
4+
from pdf2image import convert_from_path
55

66
class PDF:
77
def __init__(self):
@@ -16,17 +16,17 @@ def pdf_info(self):
1616
def split_pdf_each(self):
1717
for infn in self.infn:
1818
try:
19-
pdf_name = infn.split('\\')[-1];
20-
dir_ = infn.replace(pdf_name, pdf_name[:-4] + '\\')
21-
if not os.path.exists(dir_):
22-
os.mkdir(dir_)
19+
(dir_, pdf_name) = os.path.split(infn)
2320
pdf_input = PdfFileReader(open(infn, 'rb'))
2421
pages = pdf_input.getNumPages()
2522
self.message = '正在拆分...'
2623
for i in range(pages):
2724
pdf_output = PdfFileWriter()
2825
pdf_output.addPage(pdf_input.getPage(i))
29-
pdf_output.write(open(dir_ + pdf_name[:-4] + '-' + str(i + 1) + '.pdf', 'wb'))
26+
path = dir_ + '/' + pdf_name[:-4] + '单页拆分'
27+
if not os.path.exists(path):
28+
os.mkdir(path)
29+
pdf_output.write(open(path + '/' + pdf_name[:-4] + '-' + str(i + 1) + '.pdf', 'wb'))
3030
self.message = pdf_name + ': ' + str(i) + '/' + str(pages)
3131
self.message = '完成'
3232
except:
@@ -35,13 +35,17 @@ def split_pdf_each(self):
3535
def split_pdf_parts(self):
3636
for infn in self.infn:
3737
try:
38+
(dir_, pdf_name) = os.path.split(infn)
3839
pdf_input = PdfFileReader(open(infn, 'rb'))
3940
self.message = '正在拆分...'
4041
for part in self.params:
4142
pdf_output = PdfFileWriter()
4243
for i in range(part[0] - 1, part[1]):
4344
pdf_output.addPage(pdf_input.getPage(i));
44-
pdf_output.write(open(infn[:-4] + '-' + str(part[0]) + '-' + str(part[1]) + '.pdf', 'wb'))
45+
path = dir_ + '/' + pdf_name[:-4] + '部分拆分'
46+
if not os.path.exists(path):
47+
os.mkdir(path)
48+
pdf_output.write(open(path + '/' + pdf_name[:-4] + '-' + str(part[0]) + '-' + str(part[1]) + '.pdf', 'wb'))
4549
self.message = '第%d部分已拆分'%(self.params.index(part) + 1)
4650
self.message = '完成'
4751
except:
@@ -127,23 +131,15 @@ def add_watermark(self):
127131
except:
128132
self.message = '出错了,请检查输入格式是否正确(page-number.pdf文件要求和程序在同一目录)'
129133

130-
def convert_pdf2_image(self):
134+
def pdf2image(self):
131135
self.message = '正在转换,所需时间较长,请稍等'
132-
zoom, type_ = self.params
133136
for infn in self.infn:
134137
try:
135-
pdf_name = infn.split('\\')[-1];
136-
dir_ = infn.replace(pdf_name, pdf_name[:-4] + '\\')
137-
if not os.path.exists(dir_):
138-
os.mkdir(dir_)
139-
doc = fitz.open(infn)
140-
for pg in range(doc.pageCount):
141-
page = doc[pg]
142-
rotate = int(0)
143-
trans = fitz.Matrix(zoom, zoom).preRotate(rotate)
144-
pm = page.getPixmap(matrix=trans, alpha=False)
145-
pm.writePNG( dir_ + pdf_name + '-' + str(pg) + '.' + type_ )
146-
self.message = pdf_name + ': ' + str(pg + 1) + '/' + str(doc.pageCount) ;
138+
(dir_, pdf_name) = os.path.split(infn)
139+
outfn = dir_ + pdf_name[:-4] + 'images/'
140+
if not os.path.exists(outfn):
141+
os.mkdir(outfn)
142+
images = convert_from_path(infn, dpi=self.params, output_folder=outfn, fmt='png', thread_count=5)
147143
self.message = '完成';
148144
except:
149145
self.message = '出错: 如输入格式无误, 则不支持此文件'

README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,49 @@
1111

1212
# 相关库
1313
1. pdf文档处理: PyPDF2.
14-
2. UI:wx.
15-
3. exe文件:pyinstaller, pywin32, pywin32-ctypes.
14+
2. pdf转图片: pdf2image.
15+
3. word转pdf: pywin32, 调用Office.
16+
4. UI:wxpython.
17+
5. exe文件:pyinstaller.
1618

1719

1820
# 打包方法
19-
首先安装上述三个库,其中pyinstaller可以官网下载压缩包,然后把待打包程序放在其解压文件夹下,然后命令行运行
20-
python pyinstaller.py -F -w client.py
21+
python需要3.6版本
22+
首先安装上述库,其中pyinstaller可以官网下载压缩包,然后把待打包程序放在其解压文件夹下,然后命令行运行
23+
pyinstaller -F -w xx/xx/app.py
2124
其中-F, -w分别表示打包为单个执行exe程序,不显示命令行窗口。
25+
26+
27+
28+
@author JMx
29+
date 2018-04-26
30+
pdf文件拆分,合并,剪切等功能.
31+
32+
1 将pdf文件拆分成单页.
33+
split_pdf_each(infn):
34+
"infn"表示文件名(含路径).
35+
例:split_pdf_each('F:/pdf/test.pdf').
36+
37+
2 将pdf文件拆分成多个部分.
38+
split_pdf_parts(infn, parts):
39+
"infn"为文件名(含路径).
40+
"parts"为每个部分起始页码列表.
41+
例:split_pdf_parts('F:/p/1.pdf',[(1,3),(40,50)]).
42+
43+
3 将多个pdf文件合并成一个.
44+
merge_pdf(infnList, outfn):
45+
"infnList"为要合并的文件名(含路径)列表.
46+
"outfn"为合并后文件名(含路径).
47+
例:merge_pdf(['F:/1.pdf','F:/2.pdf'], 'F:/12.pdf').
48+
49+
4 将pdf文件页面剪切至合适大小.
50+
cut_pdf(infn, left, right, lower, upper, option, isTest):
51+
"infn"表示文件名(含路径).
52+
2~5四个参数分别是四周需要剪去的宽度(一般在10~160).
53+
"option"为剪切方式,all/odd/even 页.
54+
"isTest"表示是否先对第一页进行测试.
55+
例:cut_pdf('F:/1.pdf',20,20,20,20,'even',1).测试.
56+
只剪切前两页,单独生成一个pdf文件.
57+
cut_pdf('F:/1.pdf',20,20,20,20,'odd',0)不测试.
58+
剪切奇数页.
59+

Readme.txt

Lines changed: 0 additions & 31 deletions
This file was deleted.

app.ico

4.19 KB
Binary file not shown.

app.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import wx, time, re, os
2+
from PDF import PDF
3+
from word2pdf import Word2PDF
4+
from threading import Thread
5+
6+
pdfdo = PDF()
7+
word2pdf = Word2PDF()
8+
9+
app = wx.App()
10+
frm = wx.Frame(None, title="pdf处理器", size = (600, 470));
11+
message = False
12+
13+
def split_fn(fn):
14+
word2pdf.infn += list( filter(lambda f: os.path.splitext(f)[1] in ['.doc', '.docx'] , fn) )
15+
pdfdo.infn += list( filter(lambda f: os.path.splitext(f)[1] == '.pdf' , fn) )
16+
message.SetValue('PDF文档信息: ' + str(pdfdo.pdf_info()) )
17+
fn = [f.split("\\")[-1] for f in (word2pdf.infn + pdfdo.infn)]
18+
wx.FindWindowById(0).SetValue(str(fn));
19+
20+
def select_files():
21+
if fileDialog.ShowModal() == wx.ID_OK:
22+
path = fileDialog.GetDirectory()
23+
fn = [ path + '\\' + f for f in fileDialog.GetFilenames() ]
24+
split_fn(fn)
25+
26+
def update_state():
27+
message.SetValue(pdfdo.message or word2pdf.message)
28+
message2 = pdfdo.message + word2pdf.message;
29+
if ('完成' not in message2) and ('出错了' not in message2):
30+
time.sleep(1)
31+
update_state()
32+
elif message2:
33+
print(message2)
34+
message.SetValue(pdfdo.message or word2pdf.message)
35+
pdfdo.message = '';
36+
word2pdf.message = '';
37+
38+
def btn_callback(event):
39+
id_ = event.GetId()
40+
41+
if id_ == 1:
42+
select_files()
43+
return
44+
if ( not(pdfdo.infn) and (id_ in list(range(1, 17, 2))) ) or ( not(word2pdf.infn) and id_ == 17 ):
45+
message.SetValue('请选择文件')
46+
return
47+
48+
pdfdo.message = ''
49+
param_ = wx.FindWindowById(id_ - 1).GetValue()
50+
param = []
51+
if id_ not in [3, 7, 13, 17]:
52+
try:
53+
param = eval(param_)
54+
except:
55+
message.SetValue('输入格式有误')
56+
return
57+
58+
Thread(target = update_state).start()
59+
60+
pdfdo.params = param;
61+
62+
if id_ == 3:
63+
pdfdo.split_pdf_each()
64+
elif id_ == 5:
65+
pdfdo.split_pdf_parts()
66+
elif id_ == 7:
67+
(path, filename) = os.path.split(pdfdo.infn[0])
68+
merge_fn = wx.FindWindowById(6).GetValue() or '合并文件.pdf';
69+
pdfdo.params = path + '\\' + merge_fn
70+
pdfdo.merge_pdf()
71+
elif id_ == 9:
72+
pdfdo.cut_pdf()
73+
elif id_ == 11:
74+
pdfdo.rotate_pdf()
75+
elif id_ == 13:
76+
pdfdo.add_watermark()
77+
elif id_ == 15:
78+
pdfdo.pdf2image()
79+
elif id_ == 17:
80+
word2pdf.run()
81+
82+
def create_gui():
83+
# input/button: pos = (left, top), size = (width, height).
84+
left = 15
85+
width = 450
86+
top = 20
87+
margin = 40
88+
height = 30
89+
labels = ['选择文件', '拆分每页', '部分拆分', '文件合并', '文件剪切', '文件旋转', '添加页码', '转为图片', 'Word转PDF']
90+
default_values = [
91+
'支持拖入文件',
92+
'支持多个文件',
93+
'支持多个文件, 如:[(1,3),(20,25),(30,40)]',
94+
'合并后文件名.pdf',
95+
'支持单个文件,如:[10,20,10,20,"even",1] (注:左, 右, 下, 上, odd/even/all, 0/1: 0为全部, 1为测试10张)',
96+
'支持单个文件,如:[90,1] (注:旋转度数是90的整数倍, 0/1: 1为测试一张, 0为全部)',
97+
'支持多个文件',
98+
'支持多个文件: 如200, 数值越大,图片越清晰,转换也越慢',
99+
'支持多个文件'
100+
]
101+
length = len(labels)
102+
103+
fileDialog = wx.FileDialog(frm, message = '选择文件', wildcard = '*.pdf;*.doc;*.docx', style = wx.FD_OPEN | wx.FD_MULTIPLE, pos = (200, 30), size = (100, 25))
104+
105+
for i in range(length):
106+
wx.TextCtrl(frm, id = 2 * i, value = default_values[i], pos = (left, top + margin * i), size = (width, height))
107+
wx.Button(frm, id = 2 * i + 1, label = labels[i], pos = (width + 20, top + margin * i), size = (100, height)).Bind(wx.EVT_BUTTON, btn_callback)
108+
109+
message = wx.TextCtrl(frm, value = "状态框", pos = (left, top + margin * length + 5), size = (555, height))
110+
return (fileDialog, message)
111+
112+
class FileDrop(wx.FileDropTarget):
113+
114+
def __init__(self, window):
115+
wx.FileDropTarget.__init__(self)
116+
self.window = window
117+
118+
def OnDropFiles(self, x, y, fn):
119+
fn = [f.replace('\\', '\\\\') for f in fn]
120+
split_fn(fn)
121+
return True
122+
123+
(fileDialog, message) = create_gui()
124+
drop = FileDrop(wx.FindWindowById(0));
125+
wx.FindWindowById(0).SetDropTarget(drop);
126+
127+
frm.Center()
128+
frm.Show()
129+
app.MainLoop()
130+
131+
132+
133+

pdf-page-size-2-a4.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

pdf-zoom.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)