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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| import sys import os from PyQt5.QtWidgets import QWidget, QApplication, QGroupBox, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout, QLineEdit, QTextEdit, QInputDialog, QFileDialog, QMessageBox, QDesktopWidget from PyQt5.QtGui import QIntValidator, QDoubleValidator, QRegExpValidator, QTextCursor, QIcon, QPixmap from PyQt5.QtCore import QRegExp from PIL import Image import docx
class ImgCut(QWidget):
def __init__(self): super(ImgCut, self).__init__()
self.forder_label = QLabel('文档位置:') self.forder_text = QLineEdit() self.forder_text.setDisabled(True) self.forder_btn = QPushButton('选择文档位置…')
self.foot_label = QLabel('关键词:') self.foot_text = QLineEdit() self.foot_text.setPlaceholderText('多个关键词之间使用英文半角,分割')
self.nums_label = QLabel('替换后:') self.nums_text = QLineEdit() self.nums_text.setPlaceholderText('与关键词一致,一一对应')
self.submit_btn = QPushButton('开始替换') self.submit_btn.setStyleSheet("QPushButton{padding:20px 4px}")
self.img_label = QLabel()
self.copy_btn = QPushButton('复制代码') self.copy_btn.setStyleSheet("QPushButton{padding:20px 4px}") self.res_teatarea = QTextEdit()
self.initUi()
def initUi(self): self.createGridGroupBox() self.creatFormGroupBox() mainLayout = QVBoxLayout() hboxLayout = QHBoxLayout() hboxLayout.addWidget(self.gridGroupBox) mainLayout.addLayout(hboxLayout) mainLayout.addWidget(self.formGroupBox) self.setLayout(mainLayout)
def createGridGroupBox(self): self.gridGroupBox = QGroupBox("基本参数") layout = QGridLayout()
self.forder_btn.clicked.connect(self.savePath) self.submit_btn.clicked.connect(self.submit) layout.setSpacing(10) layout.addWidget(self.forder_label, 1, 0) layout.addWidget(self.forder_text, 1, 1) layout.addWidget(self.forder_btn, 1, 2)
layout.addWidget(self.foot_label, 2, 0) layout.addWidget(self.foot_text, 2, 1, 1, 2) layout.addWidget(self.nums_label, 3, 0) layout.addWidget(self.nums_text, 3, 1, 1, 2)
layout.addWidget(self.submit_btn, 4, 0, 1, 3) layout.setColumnStretch(1, 10) self.gridGroupBox.setLayout(layout) self.setWindowTitle('word替换_Anthor By Tony Yu') self.setWindowIcon(QIcon(r'E:\site\python\cutimg\favicon.ico'))
def creatVboxGroupBox(self): self.vboxGroupBox = QGroupBox("图片预览") layout = QVBoxLayout() pixmap = QPixmap(r"E:\site\python\cutimg\default.jpg").scaled(300, 300) self.img_label.setPixmap(pixmap) layout.addWidget(self.img_label) self.vboxGroupBox.setLayout(layout)
def creatFormGroupBox(self): self.formGroupBox = QGroupBox("替换结果") layout = QGridLayout()
layout.addWidget(self.res_teatarea, 1, 0) self.copy_btn.clicked.connect(self.copyText)
self.formGroupBox.setLayout(layout)
def showMsg(self, tit, content, icon=3): box = QMessageBox(QMessageBox.Question, tit, content) box.setWindowIcon(QIcon(r'E:\site\python\cutimg\favicon.ico'))
yes = box.addButton('确定', QMessageBox.YesRole) box.setIcon(icon) box.exec()
def savePath(self): path = QFileDialog.getExistingDirectory(self, "请选择您要保存的位置") if os.path.exists(path): self.forder_text.setText(path) else: self.showMsg('错误', '您选择的保存路径不存在,请重新选择!')
def submit(self): forder = self.forder_text.text() if not forder: self.showMsg('错误', '您还没有选择文档所在目录') return False
before_words = self.foot_text.text() if before_words: before_words = before_words.split(',') else: self.showMsg('错误', '请填写要替换的关键词')
after_words = self.nums_text.text().split(',')
files = self.get_files(forder)
for file in files:
doc = docx.Document(file)
if len(before_words) == len(after_words): for index, word in enumerate(before_words): self.replace_word(doc, word, after_words[index]) else: for word in before_words: self.replace_word(doc, word, after_words[0]) doc.save("{}/{}".format(forder, file.split("/")[-1])) self.res_teatarea.moveCursor(QTextCursor.End) shtml = "{}替换完成\n".format(file) self.res_teatarea.insertPlainText(shtml)
def get_files(self, forder): files = [] for file in os.listdir(forder): if file.endswith(".docx"): files.append(forder+'/'+file) return files def replace_word(self, doc, before_words, after_words): '''此函数用于批量替换合同中需要替换的信息 doc:文件 old_info和new_info:原文字和需要替换的新文字 ''' for para in doc.paragraphs: for run in para.runs: run.text = run.text.replace(before_words, after_words) for table in doc.tables: for row in table.rows: for cell in row.cells: cell.text = cell.text.replace(before_words, after_words)
def copyText(self): data = self.res_teatarea.toPlainText() if data: clipboard = QApplication.clipboard() clipboard.setText(data) self.showMsg('信息', '内容以成功复制到剪贴板', 1) else: self.showMsg('错误', '代码为空,没有代码可以复制') def cutImg(self, file, head, foot, pre, forder, nums): img = Image.open(file) width, height = img.size item_height = (height - head - foot) / nums for i in range(nums): croped = img.crop((0, (head + i * item_height), width, (i + 1) * item_height + head)) i += 1 if i < 10: i = '0{}'.format(i) imgName = '{}{}.jpg'.format(pre, i) self.res_teatarea.moveCursor(QTextCursor.End) shtml = '<img src="{}" />\n'.format(imgName) self.res_teatarea.insertPlainText(shtml) files = os.path.join(forder, imgName) croped.save(files) self.showMsg('成功', '图片切割完成,请到目标文件夹查看吧', 1)
if __name__ == '__main__': app = QApplication(sys.argv) ex = ImgCut() ex.show() sys.exit(app.exec_())
|