Skip to content

Commit

Permalink
add word2pdf in gui
Browse files Browse the repository at this point in the history
  • Loading branch information
jiandandaoxingfu committed Jun 28, 2022
1 parent 9825dd1 commit d6e2649
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 298 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pids
*.pid
*.seed
*.pid.lock
*.spec

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
Expand Down
Binary file removed A4.pdf
Binary file not shown.
38 changes: 17 additions & 21 deletions PDF.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from PyPDF2 import PdfFileReader, PdfFileWriter
import sys
import os
import fitz
from pdf2image import convert_from_path

class PDF:
def __init__(self):
Expand All @@ -16,17 +16,17 @@ def pdf_info(self):
def split_pdf_each(self):
for infn in self.infn:
try:
pdf_name = infn.split('\\')[-1];
dir_ = infn.replace(pdf_name, pdf_name[:-4] + '\\')
if not os.path.exists(dir_):
os.mkdir(dir_)
(dir_, pdf_name) = os.path.split(infn)
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(dir_ + pdf_name[:-4] + '-' + str(i + 1) + '.pdf', 'wb'))
path = dir_ + '/' + pdf_name[:-4] + '单页拆分'
if not os.path.exists(path):
os.mkdir(path)
pdf_output.write(open(path + '/' + pdf_name[:-4] + '-' + str(i + 1) + '.pdf', 'wb'))
self.message = pdf_name + ': ' + str(i) + '/' + str(pages)
self.message = '完成'
except:
Expand All @@ -35,13 +35,17 @@ def split_pdf_each(self):
def split_pdf_parts(self):
for infn in self.infn:
try:
(dir_, pdf_name) = os.path.split(infn)
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'))
path = dir_ + '/' + pdf_name[:-4] + '部分拆分'
if not os.path.exists(path):
os.mkdir(path)
pdf_output.write(open(path + '/' + pdf_name[:-4] + '-' + str(part[0]) + '-' + str(part[1]) + '.pdf', 'wb'))
self.message = '第%d部分已拆分'%(self.params.index(part) + 1)
self.message = '完成'
except:
Expand Down Expand Up @@ -127,23 +131,15 @@ def add_watermark(self):
except:
self.message = '出错了,请检查输入格式是否正确(page-number.pdf文件要求和程序在同一目录)'

def convert_pdf2_image(self):
def pdf2image(self):
self.message = '正在转换,所需时间较长,请稍等'
zoom, type_ = self.params
for infn in self.infn:
try:
pdf_name = infn.split('\\')[-1];
dir_ = infn.replace(pdf_name, pdf_name[:-4] + '\\')
if not os.path.exists(dir_):
os.mkdir(dir_)
doc = fitz.open(infn)
for pg in range(doc.pageCount):
page = doc[pg]
rotate = int(0)
trans = fitz.Matrix(zoom, zoom).preRotate(rotate)
pm = page.getPixmap(matrix=trans, alpha=False)
pm.writePNG( dir_ + pdf_name + '-' + str(pg) + '.' + type_ )
self.message = pdf_name + ': ' + str(pg + 1) + '/' + str(doc.pageCount) ;
(dir_, pdf_name) = os.path.split(infn)
outfn = dir_ + pdf_name[:-4] + 'images/'
if not os.path.exists(outfn):
os.mkdir(outfn)
images = convert_from_path(infn, dpi=self.params, output_folder=outfn, fmt='png', thread_count=5)
self.message = '完成';
except:
self.message = '出错: 如输入格式无误, 则不支持此文件'
46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,49 @@

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


# 打包方法
首先安装上述三个库,其中pyinstaller可以官网下载压缩包,然后把待打包程序放在其解压文件夹下,然后命令行运行
python pyinstaller.py -F -w client.py
python需要3.6版本
首先安装上述库,其中pyinstaller可以官网下载压缩包,然后把待打包程序放在其解压文件夹下,然后命令行运行
pyinstaller -F -w xx/xx/app.py
其中-F, -w分别表示打包为单个执行exe程序,不显示命令行窗口。



@author JMx
date 2018-04-26
pdf文件拆分,合并,剪切等功能.

1 将pdf文件拆分成单页.
split_pdf_each(infn):
"infn"表示文件名(含路径).
例:split_pdf_each('F:/pdf/test.pdf').

2 将pdf文件拆分成多个部分.
split_pdf_parts(infn, parts):
"infn"为文件名(含路径).
"parts"为每个部分起始页码列表.
例:split_pdf_parts('F:/p/1.pdf',[(1,3),(40,50)]).

3 将多个pdf文件合并成一个.
merge_pdf(infnList, outfn):
"infnList"为要合并的文件名(含路径)列表.
"outfn"为合并后文件名(含路径).
例:merge_pdf(['F:/1.pdf','F:/2.pdf'], 'F:/12.pdf').
4 将pdf文件页面剪切至合适大小.
cut_pdf(infn, left, right, lower, upper, option, isTest):
"infn"表示文件名(含路径).
2~5四个参数分别是四周需要剪去的宽度(一般在10~160).
"option"为剪切方式,all/odd/even 页.
"isTest"表示是否先对第一页进行测试.
例:cut_pdf('F:/1.pdf',20,20,20,20,'even',1).测试.
只剪切前两页,单独生成一个pdf文件.
cut_pdf('F:/1.pdf',20,20,20,20,'odd',0)不测试.
剪切奇数页.

31 changes: 0 additions & 31 deletions Readme.txt

This file was deleted.

Binary file added app.ico
Binary file not shown.
133 changes: 133 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import wx, time, re, os
from PDF import PDF
from word2pdf import Word2PDF
from threading import Thread

pdfdo = PDF()
word2pdf = Word2PDF()

app = wx.App()
frm = wx.Frame(None, title="pdf处理器", size = (600, 470));
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 = [ path + '\\' + f for f in fileDialog.GetFilenames() ]
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:
print(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]:
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()

def create_gui():
# input/button: pos = (left, top), size = (width, height).
left = 15
width = 450
top = 20
margin = 40
height = 30
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, 数值越大,图片越清晰,转换也越慢',
'支持多个文件'
]
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):
wx.TextCtrl(frm, id = 2 * i, value = default_values[i], pos = (left, top + margin * i), size = (width, height))
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)

message = wx.TextCtrl(frm, value = "状态框", pos = (left, top + margin * length + 5), size = (555, height))
return (fileDialog, message)

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);

frm.Center()
frm.Show()
app.MainLoop()




54 changes: 0 additions & 54 deletions pdf-page-size-2-a4.py

This file was deleted.

22 changes: 0 additions & 22 deletions pdf-zoom.py

This file was deleted.

Loading

0 comments on commit d6e2649

Please sign in to comment.