module_move_files.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from UI.move_files import Ui_Form as move_files_Ui_Form
  2. from PySide6.QtWidgets import *
  3. from PySide6.QtCore import *
  4. import settings
  5. import os
  6. import threading
  7. import shutil
  8. import time
  9. """
  10. 图片移动
  11. """
  12. class MoveFiles(QWidget, move_files_Ui_Form):
  13. progress_sign = Signal(dict)
  14. info_sign = Signal(str)
  15. text_show = Signal(str)
  16. def __init__(self, windows):
  17. super().__init__()
  18. self.windows = windows
  19. # 加载默认配置
  20. self.to_deal_image_dir = ""
  21. self.setupUi(self)
  22. # 0禁用 1进行中 2已结束
  23. self.state = 2
  24. self.init()
  25. self.setFixedSize(self.width(), self.height())
  26. self.setWindowModality(Qt.ApplicationModal)
  27. self.show()
  28. def init(self):
  29. self.label_3.mousePressEvent = self.change_image_dir
  30. self.textBrowser.setText(self.get_len_text(self.to_deal_image_dir, 50))
  31. self.pushButton.clicked.connect(self.run)
  32. # self.pushButton_2.clicked.connect(lambda: os.startfile(os.getcwd() + "/" + "out_put"))
  33. def get_len_text(self, text, max_len: int):
  34. if len(text) > max_len:
  35. text = text[:int(max_len / 4)] + "..." + text[-1 * int(max_len * 3 / 4):]
  36. return text
  37. def change_image_dir(self, *args):
  38. pic_dir = QFileDialog.getExistingDirectory(None, "选取图片文件夹", "")
  39. if not pic_dir:
  40. self.textBrowser.setText("")
  41. self.to_deal_image_dir = ""
  42. return
  43. self.to_deal_image_dir = pic_dir
  44. self.textBrowser.setText(pic_dir)
  45. def check_path(self, _path):
  46. if not os.path.exists(_path):
  47. os.mkdir(_path)
  48. return True
  49. def set_state(self, state_value: int):
  50. # 0禁用 1进行中 2已结束
  51. if state_value not in [0, 1, 2]:
  52. return
  53. self.state = state_value
  54. if self.state == 0:
  55. self.pushButton.setEnabled(False)
  56. if self.state == 1:
  57. self.pushButton.setEnabled(False)
  58. if self.state == 2:
  59. self.pushButton.setEnabled(True)
  60. def check_file_is_closed(self, file_path):
  61. try:
  62. s = int(time.time())
  63. _name, extension = os.path.splitext(file_path)
  64. _temp_file_name = "{_name}_{_time}{extension}".format(_name=_name,
  65. _time=s,
  66. extension=extension)
  67. os.rename(file_path, _temp_file_name)
  68. os.rename(_temp_file_name, file_path)
  69. return True
  70. except:
  71. return False
  72. def run(self):
  73. self.total_num = 0
  74. if not self.to_deal_image_dir:
  75. QMessageBox.question(self, '确认', '请选择待处理图片文件夹', QMessageBox.Yes)
  76. return
  77. self.textEdit_2.setText("")
  78. self.textEdit_3.setText("")
  79. text = self.textEdit.toPlainText()
  80. goods_art_list = []
  81. for i in text.split("\n"):
  82. i = i.replace(" ", "")
  83. if i:
  84. goods_art_list.append(i.upper())
  85. if not goods_art_list:
  86. QMessageBox.question(self, '确认', '请填写待转移图片', QMessageBox.Yes)
  87. return
  88. to_deal_pic = []
  89. _Type = ['.png', '.PNG', '.jpg', '.JPG', '.jpge', '.JPGE', '.gif', '.GIF']
  90. for pic in os.listdir(self.to_deal_image_dir):
  91. pic_path = "{}/{}".format(self.to_deal_image_dir, pic)
  92. if os.path.isfile(pic_path):
  93. if os.path.splitext(pic)[1] in _Type:
  94. to_deal_pic.append(pic)
  95. if not to_deal_pic:
  96. QMessageBox.question(self, '确认', '该文件夹下没有任何图片', QMessageBox.Yes)
  97. return
  98. # 开始执行拼接处理
  99. # self.r = RunExcel(self)
  100. # self.r.start()
  101. self.set_state(state_value=1)
  102. self.t = threading.Thread(target=self.run_by_thread, args=())
  103. self.t.start()
  104. def run_by_thread(self):
  105. out_path = "{}/已匹配".format(self.to_deal_image_dir)
  106. self.check_path(out_path)
  107. # 待处理图片
  108. to_deal_pic = {}
  109. _Type = ['.png', '.PNG', '.jpg', '.JPG', '.jpge', '.JPGE', '.gif', '.GIF']
  110. for pic in os.listdir(self.to_deal_image_dir):
  111. pic_path = "{}/{}".format(self.to_deal_image_dir, pic)
  112. if os.path.isfile(pic_path):
  113. if os.path.splitext(pic)[1] in _Type:
  114. to_deal_pic[os.path.splitext(pic)[0].upper()] = pic_path
  115. text = self.textEdit.toPlainText()
  116. goods_art_list = []
  117. for i in text.split("\n"):
  118. i = i.replace(" ", "")
  119. if i:
  120. goods_art_list.append(i.upper())
  121. for goods_art_no in goods_art_list:
  122. print("开始处理:{}".format(goods_art_no))
  123. if goods_art_no in to_deal_pic:
  124. print("开始移动:{}".format(to_deal_pic[goods_art_no]))
  125. shutil.move(to_deal_pic[goods_art_no], out_path)
  126. self.textEdit_2.append(goods_art_no)
  127. else:
  128. self.textEdit_3.append(goods_art_no)
  129. self.set_state(state_value=2)
  130. print("----------已完成----------")
  131. def column_to_letter(self, column_index):
  132. """
  133. 将列位置转换为对应的字母
  134. """
  135. letter = ''
  136. while column_index > 0:
  137. column_index -= 1
  138. letter = chr(column_index % 26 + 65) + letter
  139. column_index //= 26
  140. return letter
  141. def append_text_to_browser(self, text):
  142. self.textBrowser_2.append(text)
  143. def create_folder(self, path):
  144. if not os.path.exists(path):
  145. os.makedirs(path)
  146. return False