Python 寫了一個批量生成資料夾和批量重新命名的工具

鹹魚Doyoung發表於2020-12-23

Python 寫了一個批量生成資料夾和批量重新命名的工具

演示

Python 寫了一個批量生成資料夾和批量重新命名的工具

功能

1. 可以讀取excel內容,使用excel單元格內容進行新建資料夾,和資料夾重新命名

2. 可以自定義重新命名

3. 等

程式碼

import os
from pathlib import Path
import xlwings as xw


tipStr = '輸入工作路徑'
tipStr1 = '輸入excel名稱'

# 主介面
def mainWindow():
    os.system('cls')
    print('1. 新建資料夾')
    print('2. 資料夾重新命名')
    print('0. 退出')
    print('輸入命令號')
    num = input()
    if('1' == num or '2' == num):
        return int(num)
    elif '0' == num:
        return 0
    else:
        return -1

# 最大最小值數字


def getMinMaxNum():
    print('輸入最小值')
    min = 0
    while True:
        min = input()
        # 判斷是否是寫數字
        if min.isdigit():
            min = int(min)
            if -1 < min and 999 >= min:
                break
            else:
                print('請輸入0-999的數字')
                continue
        else:
            print('請輸入數字')
            continue

    print('輸入最大值')
    max = 0
    while True:
        max = input()
        if max.isdigit():
            max = int(max)
            if -1 < max and 999 >= max:
                break
            else:
                print('請輸入0-999的數字')
                continue
        else:
            print('請輸入數字')
            continue
    return min, max

# 獲取excel內容


def getExcelContent(column):
    path = os.getcwd()

    print(tipStr1)
    excelPath = ''
    flag = 0
    while True:
        # 拼接excel路徑
        excelPath = path + '\\' + input()
        if not excelPath.endswith('.xlsx'):
            excelPath = excelPath + ".xlsx"
        # 判斷檔案是否存在
        if Path(excelPath).exists():
            break
        else:
            if 3 <= flag:
                input('錯誤次數超過3次,重新開始,回車繼續')
                return False, []
            print('檔案不存在,重新輸入')
            flag = flag + 1
    # 指定不顯示地開啟Excel,讀取Excel檔案
    app = xw.App(visible=False, add_book=False)
    wb = app.books.open(excelPath)  # 開啟Excel檔案
    sheet = wb.sheets[0]  # 選擇第0個表單
    # 獲取錶行數
    sheetInfo = sheet.used_range
    maxRow = sheetInfo.last_cell.row
    # print('行數:', maxRow)
    nameList = []
    # 遍歷行,儲存行資料
    for row in range(1, maxRow + 1):
        value = sheet.range(str(column)+str(row)).value
        if isinstance(value, float):
            nameList.append(str(int(value)))
        else:
            nameList.append(value)

    # 關閉當前工作簿
    wb.close()
    # 退出excel程式
    app.quit()

    return True, nameList

# 獲取起始數字


def getStartNum():
    # 獲取起始數字
    startNum = 0
    while True:
        print('輸入起始數字')
        startNum = input()
        if not startNum.isdigit():
            input("請輸入數字,回車繼續")
            continue
        break
    # 轉換為int型別
    startNum = int(startNum)
    return startNum

# 新建資料夾
def newDir():
    # 插入標籤
    os.system('cls')
    print('1. excel\'A\'新建 (說明:將從指定的excel\'A\'列讀取內容,讀取的單元格內容做為資料夾名稱)')
    print('2. 0-999序號新建 (說明:將新建指定序列的資料夾)')
    print('3. xxx0-999字首加序號新建 (例:我很帥1,我很帥2...)')
    print('4. 0-999xxx序號加字尾新建 (例:1我很帥,2我很帥...)')
    print('輸入命令號')
    order = input()
    if '1' == order:
        os.system('cls')
        path = os.getcwd()

        state, nameList = getExcelContent('A')
        if not state:
            return
        for name in nameList:
            dirName = path + '\\' + name
            if Path(dirName).exists():
                continue
            os.mkdir(dirName)
            print('建立目錄【%s】成功' % dirName)
        input("回車繼續")
    elif '2' == order:
        os.system('cls')
        min, max = getMinMaxNum()
        print('輸入最小值')
        for index in range(min, max + 1):
            path = os.getcwd()
            if Path(path + '\\' + str(index)).exists():
                continue
            os.mkdir(path + '\\' + str(index))
    elif '3' == order:
        os.system('cls')
        print('輸入字首')
        prefix = input()
        # 獲取最大最小值
        min, max = getMinMaxNum()
        # 建立資料夾
        for index in range(min, max+1):
            path = os.getcwd()
            dirName = path + '\\' + prefix + str(index)
            if Path(dirName).exists():
                continue
            os.mkdir(dirName)
    elif '4' == order:
        os.system('cls')
        min, max = getMinMaxNum()
        print('輸入字尾')
        stufix = input()
        for index in range(min, max):
            path = os.getcwd()
            dirName = path + '\\' + str(index) + stufix
            if Path(dirName).exists():
                continue
            os.mkdir(dirName)
    else:
        input('無效的命令,回車繼續')

# 資料夾重新命名
def reName():
    os.system('cls')
    print('1. 從excel\'A\'列重新命名')
    print('2. 數字順序重新命名')
    print('3. 字首加數字順序重新命名')
    print('4. 數字加字尾順序重新命名')
    print('輸入命令號')
    order = input()

    if '1' == order:
        # 當前路徑
        currentPath = os.getcwd()
        # 獲取所有目錄
        dirList = []
        for item in os.listdir(currentPath):
            if os.path.isdir(currentPath + '\\' + item):
                dirList.append(currentPath + '\\' + item)

        # 判斷當前資料夾有沒有資料夾
        if 0 == len(dirList):
            input('當前目錄不存在資料夾,請先建立')
            return
        # 獲取excel內容
        state, nameList = getExcelContent('A')
        if not state:
            return

        # 遍歷所有資料夾進行重新命名
        for index in range(0, len(nameList)):
            if index > len(dirList) - 1:
                input('所有資料夾重新命名完畢,多餘excel內容將不執行,回車繼續')
                break
            oldDirName = dirList[index]
            newDirName = currentPath + '\\' + nameList[index]
            os.rename(oldDirName, newDirName)

    elif '2' == order:
        # 當前路徑
        currentPath = os.getcwd()
        # 獲取所有目錄
        dirList = []
        for item in os.listdir(currentPath):
            if os.path.isdir(currentPath + '\\' + item):
                dirList.append(currentPath + '\\' + item)

        # 判斷當前資料夾有沒有資料夾
        if 0 == len(dirList):
            input('當前目錄不存在資料夾,請先建立')
            return

        # 獲取起始數字
        startNum = getStartNum()

        # 重新命名
        for dirName in dirList:
            numb = startNum
            newDirName = currentPath + '\\' + str(numb)
            if Path(newDirName).exists():
                input('起始數字資料夾已存在,請嘗試其它,回車繼續')
                break
            os.rename(dirName, newDirName)
            startNum = startNum + 1

    elif '3' == order:
        # 當前路徑
        currentPath = os.getcwd()
        # 獲取所有目錄
        dirList = []
        for item in os.listdir(currentPath):
            if os.path.isdir(currentPath + '\\' + item):
                dirList.append(currentPath + '\\' + item)

        # 判斷當前資料夾有沒有資料夾
        if 0 == len(dirList):
            input('當前目錄不存在資料夾,請先建立')
            return

        # 獲取字首
        print('輸入字首')
        preFix = input()

        # 獲取起始數字
        startNum = getStartNum()

        for name in dirList:
            num = startNum
            newDirName = currentPath + '\\' + preFix + str(num)
            os.rename(name, newDirName)
            startNum = startNum + 1

    elif '4' == order:
        # 當前路徑
        currentPath = os.getcwd()
        # 獲取所有目錄
        dirList = []
        for item in os.listdir(currentPath):
            if os.path.isdir(currentPath + '\\' + item):
                dirList.append(currentPath + '\\' + item)

        # 判斷當前資料夾有沒有資料夾
        if 0 == len(dirList):
            input('當前目錄不存在資料夾,請先建立')
            return

        # 獲取起始數字
        startNum = getStartNum()

        # 獲取字尾
        print('輸入字尾')
        stufix = input()

        for name in dirList:
            num = startNum
            newDirName = currentPath + '\\' + str(num) + stufix
            os.rename(name, newDirName)
            startNum = startNum + 1

    else:
        input('無效的命令,回車繼續')


if __name__ == "__main__":
    while(True):
        num = mainWindow()
        if -1 == num:
            continue
        elif 0 == num:
            print('byebye and see you lala')
            input('任意鍵退出')
            break
        elif 1 == num:
            newDir()
        elif 2 == num:
            reName()

下載

https://download.csdn.net/download/ShiShiSoLo/13767713

相關文章