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