Last active
December 9, 2016 06:55
-
-
Save bucho666/efa9b8278ba7ffa724b5b2411ec014a2 to your computer and use it in GitHub Desktop.
文字列一括置換ツール(PySide)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: shift-jis -*- | |
import sys | |
import os | |
from PySide import QtCore | |
from PySide import QtGui | |
from replace import MultiReplace | |
class MainWindow(QtGui.QDialog): | |
def __init__(self, parent=None): | |
super(MainWindow, self).__init__(parent) | |
self.setAcceptDrops(True) | |
self.resize(480, 180) | |
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) | |
self._label = QtGui.QLabel('') | |
self._label.setAlignment(QtCore.Qt.AlignVCenter|QtCore.Qt.AlignHCenter) | |
self._multi_replace = None | |
self._reset_button = QtGui.QPushButton('リセット') | |
self._reset_button.clicked.connect(self.reset) | |
layout = QtGui.QVBoxLayout() | |
layout.addWidget(self._label) | |
layout.addWidget(self._reset_button) | |
self.setLayout(layout) | |
self.reset() | |
def reset(self): | |
self._label.setText('<font color=blue size=24>設定ファイルをドラッグ&ドロップ</font>') | |
self._multi_replace = None | |
def dropEvent(self, event): | |
path = event.mimeData().urls()[0].toLocalFile() | |
if self._multi_replace: | |
self.replace(path) | |
else: | |
self.load_multi_replace(path) | |
def replace(self, path): | |
save_file, filter = QtGui.QFileDialog.getSaveFileName(self, | |
caption='置換済みファイルの保存', | |
dir=os.path.dirname(path), | |
filter="Text File (*.txt)") | |
if not save_file: | |
return | |
self._multi_replace.replace(path, save_file) | |
self.reset() | |
def load_multi_replace(self, path): | |
self._multi_replace = MultiReplace().load(path) | |
self._label.setText('<font color=green size=24>置換対象ファイルをドラッグ&ドロップ</font>') | |
def dragEnterEvent(self,event): | |
if event.mimeData().hasUrls(): | |
event.accept() | |
else: | |
event.ignore() | |
if __name__ == '__main__': | |
app = QtGui.QApplication(sys.argv) | |
name = '一括置換ツール' | |
app.setApplicationName(name) | |
mainWindow = MainWindow() | |
mainWindow.setWindowTitle(name) | |
mainWindow.show() | |
app.exec_() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: shift-jis -* | |
class ReplaceTargets(object): | |
def __init__(self): | |
self._targets = list() | |
def load(self, file_name): | |
with open(file_name, 'r') as file: | |
lines = file.readlines() | |
self._targets = [line.strip().split('\t') for line in lines] | |
return self | |
def replace(self, str): | |
for src, dst in self._targets: | |
str = str.replace(src, dst) | |
return str | |
class MultiReplace(object): | |
def __init__(self): | |
self._replace_targets = ReplaceTargets() | |
def load(self, file): | |
self._replace_targets.load(file) | |
return self | |
def replace(self, src, dst): | |
with open(src, 'r') as file: | |
lines = file.readlines() | |
with open(dst, 'w') as file: | |
file.writelines([self._replace_targets.replace(line) for line in lines]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment