pyQT5小工具(二)移动文件到文件夹(村小组文件移动到村委会文件夹)

#无子文件夹。

from PyQt5.QtWidgets import QWidget,QApplication,QMainWindow,QAction,QMessageBox
import xlrd
import sys
import os
import shutil
import time

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication, QPushButton, QMessageBox, QRadioButton, QLabel, QLineEdit,
QComboBox, QGridLayout, QLCDNumber, QTextEdit, QFileDialog
from PyQt5.QtCore import Qt
list_orde_admi_all = []
list_orde_vill_all = []
list_admi_all = []
list_vill = []
dict_no_repe1 = {} #no-repeat不重复
orde_admi = ‘’
suf_vill = ‘’ # 因为下面suf_vill只出现在if中,所以要先定义,否则会报错

list_fold_targ = []

curn_path = os.getcwd() #current path当前路径

class moveFile(QWidget):
def init(self):
super().init()
self.initUI()
#self.setWindowOpacity(0.95) # 窗体透明度
self.path = self.ql_path_brow.text()
def initUI(self):

    self.labe_dir_save = QLabel('选择文件夹')

    self.ql_path_brow = QLineEdit()
    self.ql_path_brow.setPlaceholderText('先点击浏览,再进行其他操作')

    self.labe_grou_stan = QLabel("村组分类标准")   #village group standard村分类标准
    self.cb_grou_stan = QComboBox()
    self.cb_grou_stan.addItems(['', '三资办村组分类', '农办村组分类', '公共服务办村组分类'])  # 一次添加多个控件
    font_size = 12

    self.labe_grou_stan.setFont(QFont('SansSerif', font_size))
    self.cb_grou_stan.setFont(QFont('SansSerif', font_size))
    self.labe_dir_save.setFont(QFont('SansSerif', font_size))
    self.ql_path_brow.setFont(QFont('SansSerif', font_size))
    grid = QGridLayout()
    #设置间隔
    grid.setSpacing(30)
    # # 放置在第3行第1列,跨越2行和1列
    # grid.addWidget(cb_admi, 3, 1, 2, 1)
    grid.addWidget(self.labe_grou_stan, 1, 0)
    grid.addWidget(self.cb_grou_stan, 1, 1)
    grid.addWidget(self.labe_dir_save, 0, 0)
    grid.addWidget(self.ql_path_brow, 0, 1)
    self.bt1 = QPushButton('开      始')
    self.bt2 = QPushButton('浏览')
    self.bt3 = QPushButton('打开')
    self.bt4 = QPushButton('帮助')
    self.bt1.setFont(QFont('SansSerif', font_size))
    self.bt2.setFont(QFont('SansSerif', font_size))
    self.bt3.setFont(QFont('SansSerif', font_size))
    self.bt4.setFont(QFont('SansSerif', font_size))
    #grid.addWidget(self.bt1, 1, 0, 1, 2)
    grid.addWidget(self.bt1, 2, 1)
    grid.addWidget(self.bt2, 0, 2)
    grid.addWidget(self.bt3, 0, 3)
    grid.addWidget(self.bt4, 2, 2)
    self.bt1.clicked.connect(self.move_file)
    self.bt2.clicked.connect(self.selectPath)
    self.bt3.clicked.connect(self.openFold)
    self.bt4.clicked.connect(self.help)





    self.setLayout(grid)
    self.setGeometry(400, 400, 650, 350)
    self.setWindowTitle("村(组)文件移动到对应的村委会文件夹")

    self.show()



def move_file(self):
    curn_path2 = os.getcwd()  # current path当前路径
    print('curn_path2')
    print(curn_path2)
    for dirpath, dirnames, filenames in os.walk(curn_path):
        for dirname in dirnames:
            list_fold_targ = dirnames
    print(list_fold_targ)
    raw_file_list, raw_file_path_list = self.get_file_list(curn_path)

    text_cb_grou_stan = self.cb_grou_stan.currentText()

    #curn_path = os.getcwd()  # current path当前路径
    curn_path = os.path.join(curn_path, 'AutoMoveFile')
    file = os.path.join(curn_path, 'AutoMoveFile.xls')
    data = xlrd.open_workbook(file)
    table = data.sheets()[0]  # 0代表第一个表单
    nrows = table.nrows  # 行
    ncols = table.ncols  # 列数

    for i in range(4, nrows-1):  # nrows
        admi = table.cell(i, 3).value #administrative village
        vill_no_repe1 = table.cell(i, 6).value
        vill_no_repe2 = table.cell(i, 7).value
        vill_no_repe3 = table.cell(i, 8).value
        vill_no_repe4 = table.cell(i, 9).value
        vill_no_repe5 = table.cell(i, 10).value

        # print(orde_admi, admi, vill)
        if vill_no_repe1 != '':
            dict_no_repe1[vill_no_repe1] = admi
            if vill_no_repe2 != '':
                dict_no_repe1[vill_no_repe2] = admi
                if vill_no_repe3 != '':
                    dict_no_repe1[vill_no_repe3] = admi
                    if vill_no_repe4 != '':
                        dict_no_repe1[vill_no_repe4] = admi
                        if vill_no_repe5 != '':
                            dict_no_repe1[vill_no_repe5] = admi
            #print(dict_no_repe1)
    # print(dict_no_repe1)
    # print(len(dict_no_repe1))



    #移动没有重名的村小组
    for key in dict_no_repe1:
        for i in range(len(raw_file_list)):
            file = raw_file_list[i]

            if key in file:
                # print('if key in file')
                # print(key, file)
                admi = dict_no_repe1[key] #行政村
                file_path = raw_file_path_list[i] #源文件全路径
                #fold_targ = os.path.join(curn_path, admi)
                #print(file_path)
                for j in range(len(list_fold_targ)):
                    fold_targ = list_fold_targ[j] #目标文件夹名称
                    #print(fold_targ)
                    if admi in fold_targ:   #如行政村在目标文件夹名称字符串中

                        fold_targ_full = os.path.join(curn_path, fold_targ)  #构建目标文件夹全路径
                        path_targ = os.path.join(fold_targ_full, file)  #构建移动后文件的全路径
                        #如果移动后文件的全路径不存在且目标文件夹全路径存在,就移动源文件到目标文件夹。
                        # print(path_targ)
                        # print(fold_targ_full)
                        if (not os.path.exists(path_targ)) and (os.path.exists(fold_targ_full)):
                            shutil.move(file_path, fold_targ_full)
                            print('不重名的村小组:', key, file, admi)

    #重名的村小组,不能用字典,字典的键不能重名。
    for i in range(4, nrows-1):  # nrows
        admi = table.cell(i, 3).value #administrative village
        vill_repe1 = table.cell(i, 3).value
        # print(orde_admi, admi, vill)
        if vill_repe1 != '':
            for i in range(len(raw_file_list)):
                file = raw_file_list[i]
                if (vill_repe1 in file) and (admi in file):
                    #print(vill_repe1, file, admi, file)

                    file_path = raw_file_path_list[i]

                    for j in range(len(list_fold_targ)):
                        fold_targ = list_fold_targ[j]
                        if admi in fold_targ:
                            fold_targ_full = os.path.join(curn_path, fold_targ)
                            path_targ = os.path.join(fold_targ_full, file)
                            fold_targ = os.path.join(curn_path, admi)
                            path_targ = os.path.join(fold_targ, file)
                            if (not os.path.exists(path_targ)) and (os.path.exists(fold_targ_full)):
                                print(admi, fold_targ)
                                try:
                                    shutil.move(file_path, fold_targ_full)
                                    print('重名的村小组:', admi, vill_repe1, file, file_path, fold_targ_full)
                                except:
                                    pass


def get_file_list(self, raw_folder_path):
    # 打开文件
    dirs = os.listdir(raw_folder_path)
    raw_file_list = []
    raw_file_path_list = []
    for home, dirs, files in os.walk(raw_folder_path):
        for file_name in files:
            raw_file_list.append((file_name))
            raw_file_path_list.append(os.path.join(home, file_name))
    # print('raw_file_list')
    # print(raw_file_list)
    return raw_file_list, raw_file_path_list

def selectPath(self):
    dir_choose = QFileDialog.getExistingDirectory(self, '选择文件夹')

    if dir_choose == '':
        print('取消选择')
        return
    # self.showInfo.appendPlainText('你选择的文件夹为:{}'.format(dir_choose))
    else:
        self.ql_path_brow.setText(dir_choose)

    print(self.ql_path_brow.text())
    return dir_choose

def openFold(self):
    os.startfile(self.path)

def help(self):
    mb = QMessageBox(QMessageBox.Information, '帮助', '''
一、先点击浏览按钮选择文件夹,否则闪退。

 二、规则:移动文件到相应文件夹。
 
    移动村委会、村小组文件到对应的村委会文件夹
    
    如:甫草洋村.xls移动到前锋村委会文件夹。

     ''', QMessageBox.Ok, self)
    # mb= QLineEdit()
    mb.show()

if name==‘main’:
app=QApplication(sys.argv)
mf=moveFile()
list1=[]

app.exec_()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值