123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- from UI.move_files import Ui_Form as move_files_Ui_Form
- from PySide6.QtWidgets import *
- from PySide6.QtCore import *
- import settings
- import os
- import threading
- import shutil
- import time
- from .MineQWidget import MineQWidget
- """
- 图片移动
- """
- class MoveFiles(MineQWidget, move_files_Ui_Form):
- progress_sign = Signal(dict)
- info_sign = Signal(str)
- text_show = Signal(str)
- def __init__(self, windows):
- super().__init__()
- self.windows = windows
- # 加载默认配置
- self.to_deal_image_dir = ""
- self.setupUi(self)
- # 0禁用 1进行中 2已结束
- self.state = 2
- self.init()
- self.setFixedSize(self.width(), self.height())
- self.setWindowModality(Qt.ApplicationModal)
- self.show()
- def init(self):
- self.label_3.mousePressEvent = self.change_image_dir
- self.textBrowser.setText(self.get_len_text(self.to_deal_image_dir, 50))
- self.pushButton.clicked.connect(self.run)
- # self.pushButton_2.clicked.connect(lambda: os.startfile(os.getcwd() + "/" + "out_put"))
- def get_len_text(self, text, max_len: int):
- if len(text) > max_len:
- text = text[:int(max_len / 4)] + "..." + text[-1 * int(max_len * 3 / 4):]
- return text
- def change_image_dir(self, *args):
- pic_dir = QFileDialog.getExistingDirectory(None, "选取图片文件夹", "")
- if not pic_dir:
- self.textBrowser.setText("")
- self.to_deal_image_dir = ""
- return
- self.to_deal_image_dir = pic_dir
- self.textBrowser.setText(pic_dir)
- def check_path(self, _path):
- if not os.path.exists(_path):
- os.mkdir(_path)
- return True
- def set_state(self, state_value: int):
- # 0禁用 1进行中 2已结束
- if state_value not in [0, 1, 2]:
- return
- self.state = state_value
- if self.state == 0:
- self.pushButton.setEnabled(False)
- if self.state == 1:
- self.pushButton.setEnabled(False)
- if self.state == 2:
- self.pushButton.setEnabled(True)
- def check_file_is_closed(self, file_path):
- try:
- s = int(time.time())
- _name, extension = os.path.splitext(file_path)
- _temp_file_name = "{_name}_{_time}{extension}".format(_name=_name,
- _time=s,
- extension=extension)
- os.rename(file_path, _temp_file_name)
- os.rename(_temp_file_name, file_path)
- return True
- except:
- return False
- def run(self):
- self.total_num = 0
- if not self.to_deal_image_dir:
- self.WaringMessage("请选择待处理图片文件夹")
- return
- self.textEdit_2.setText("")
- self.textEdit_3.setText("")
- text = self.textEdit.toPlainText()
- goods_art_list = []
- for i in text.split("\n"):
- i = i.replace(" ", "")
- if i:
- goods_art_list.append(i.upper())
- if not goods_art_list:
- self.WaringMessage("请填写待转移图片")
- return
- to_deal_pic = []
- _Type = ['.png', '.PNG', '.jpg', '.JPG', '.jpge', '.JPGE', '.gif', '.GIF']
- for pic in os.listdir(self.to_deal_image_dir):
- pic_path = "{}/{}".format(self.to_deal_image_dir, pic)
- if os.path.isfile(pic_path):
- if os.path.splitext(pic)[1] in _Type:
- to_deal_pic.append(pic)
- if not to_deal_pic:
- self.WaringMessage("该文件夹下没有任何图片")
- return
- # 开始执行拼接处理
- # self.r = RunExcel(self)
- # self.r.start()
- self.set_state(state_value=1)
- self.t = threading.Thread(target=self.run_by_thread, args=())
- self.t.start()
- def run_by_thread(self):
- out_path = "{}/已匹配".format(self.to_deal_image_dir)
- self.check_path(out_path)
- # 待处理图片
- to_deal_pic = {}
- _Type = ['.png', '.PNG', '.jpg', '.JPG', '.jpge', '.JPGE', '.gif', '.GIF']
- for pic in os.listdir(self.to_deal_image_dir):
- pic_path = "{}/{}".format(self.to_deal_image_dir, pic)
- if os.path.isfile(pic_path):
- if os.path.splitext(pic)[1] in _Type:
- to_deal_pic[os.path.splitext(pic)[0].upper()] = pic_path
- text = self.textEdit.toPlainText()
- goods_art_list = []
- for i in text.split("\n"):
- i = i.replace(" ", "")
- if i:
- goods_art_list.append(i.upper())
- for goods_art_no in goods_art_list:
- print("开始处理:{}".format(goods_art_no))
- if goods_art_no in to_deal_pic:
- print("开始移动:{}".format(to_deal_pic[goods_art_no]))
- shutil.move(to_deal_pic[goods_art_no], out_path)
- self.textEdit_2.append(goods_art_no)
- else:
- self.textEdit_3.append(goods_art_no)
- self.set_state(state_value=2)
- print("----------已完成----------")
- def column_to_letter(self, column_index):
- """
- 将列位置转换为对应的字母
- """
- letter = ''
- while column_index > 0:
- column_index -= 1
- letter = chr(column_index % 26 + 65) + letter
- column_index //= 26
- return letter
- def append_text_to_browser(self, text):
- self.textBrowser_2.append(text)
- def create_folder(self, path):
- if not os.path.exists(path):
- os.makedirs(path)
- return False
|