-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
144 lines (120 loc) · 4.63 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import wx, time, re, os
import webbrowser
from PDF import PDF
from word2pdf import Word2PDF
from threading import Thread
pdfdo = PDF()
word2pdf = Word2PDF()
scale = 1
margin = int(10 * scale) # 窗口上下左右, 文本框与文本框, 与按钮之间的空白大小
textWidth = int(450 * scale) # 文本框大小
textHeight = int(30 * scale)
buttonWidth = int(100 * scale) # 按钮大小
buttonHeight = textHeight
N = 10 # 控件行数
windowWidth = textWidth + buttonWidth + margin * 3 + 15 # 窗口大小
windowHeight = (textHeight + margin) * N + margin + 41 # 71是标题高度
app = wx.App()
frm = wx.Frame(None, title="pdf处理器", size = (windowWidth, windowHeight));
message = False
def split_fn(fn):
word2pdf.infn += list( filter(lambda f: os.path.splitext(f)[1] in ['.doc', '.docx'] , fn) )
pdfdo.infn += list( filter(lambda f: os.path.splitext(f)[1] == '.pdf' , fn) )
message.SetValue('PDF文档信息: ' + str(pdfdo.pdf_info()) )
fn = [f.split("\\")[-1] for f in (word2pdf.infn + pdfdo.infn)]
wx.FindWindowById(0).SetValue(str(fn));
def select_files():
if fileDialog.ShowModal() == wx.ID_OK:
path = fileDialog.GetDirectory()
fn = fileDialog.GetFilenames()
print(fn)
split_fn(fn)
def update_state():
message.SetValue(pdfdo.message or word2pdf.message)
message2 = pdfdo.message + word2pdf.message
if ('完成' not in message2) and ('出错' not in message2):
time.sleep(1)
update_state()
elif message2:
message.SetValue(pdfdo.message or word2pdf.message)
pdfdo.message = ''
word2pdf.message = ''
def btn_callback(event):
id_ = event.GetId()
if id_ == 1:
select_files()
return
if ( not(pdfdo.infn) and (id_ in list(range(1, 17, 2))) ) or ( not(word2pdf.infn) and id_ == 17 ):
message.SetValue('请选择文件')
return
pdfdo.message = ''
param_ = wx.FindWindowById(id_ - 1).GetValue()
param = []
if id_ not in [3, 7, 13, 17, 19]:
try:
param = eval(param_)
except:
message.SetValue('输入格式有误')
return
Thread(target = update_state).start()
pdfdo.params = param;
if id_ == 3:
pdfdo.split_pdf_each()
elif id_ == 5:
pdfdo.split_pdf_parts()
elif id_ == 7:
(path, filename) = os.path.split(pdfdo.infn[0])
merge_fn = wx.FindWindowById(6).GetValue() or '合并文件.pdf';
pdfdo.params = path + '\\' + merge_fn
pdfdo.merge_pdf()
elif id_ == 9:
pdfdo.cut_pdf()
elif id_ == 11:
pdfdo.rotate_pdf()
elif id_ == 13:
pdfdo.add_watermark()
elif id_ == 15:
pdfdo.pdf2image()
elif id_ == 17:
word2pdf.run()
elif id_ == 19:
webbrowser.open('https://github.com/jiandandaoxingfu/pdfdo')
def create_gui():
# input/button: pos = (left, top), size = (width, height).
labels = ['选择文件', '拆分每页', '部分拆分', '文件合并', '文件剪切', '文件旋转', '添加页码', '转为图片', 'Word转PDF', '使用说明']
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为全部)',
'支持多个文件',
'支持多个文件, 如: 200, 数值越大,图片越清晰,转换也越慢',
'支持多个文件(电脑需要安装有 Microsoft Word)',
'状态框'
]
length = len(labels)
fileDialog = wx.FileDialog(frm, message = '选择文件', wildcard = '*.pdf;*.doc;*.docx', style = wx.FD_OPEN | wx.FD_MULTIPLE, pos = (200, 30), size = (100, 25))
for i in range(length):
top_ = margin + (textHeight + margin) * i
wx.TextCtrl(frm, id = 2 * i, value = default_values[i], pos = (margin, top_), size = (textWidth, textHeight))
wx.Button(frm, id = 2 * i + 1, label = labels[i], pos = (textWidth + 2 * margin, top_), size = (buttonWidth, buttonHeight)).Bind(wx.EVT_BUTTON, btn_callback)
# message = wx.TextCtrl(frm, value = "状态框", pos = (left, top + margin * length + 5), size = (555, height))
return (fileDialog, wx.FindWindowById(length * 2 - 2))
class FileDrop(wx.FileDropTarget):
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, fn):
fn = [f.replace('\\', '\\\\') for f in fn]
split_fn(fn)
return True
(fileDialog, message) = create_gui()
drop = FileDrop(wx.FindWindowById(0));
wx.FindWindowById(0).SetDropTarget(drop);
# import wx.lib.inspection
# wx.lib.inspection.InspectionTool().Show()
frm.Center()
frm.Show()
app.MainLoop()