Python-以excel特定列爲參考,查找缺少的文件

本模塊主要實現功能:參照excel表某列的內容,查找指定文件夾中的去掉後綴的文件名稱是否存在。python 讀取excel的package(excel read data):xlrd

import xlrd
import os
from find_lack_file2 import folder_directory

# excel路徑輸入判斷
def excel_directory(path):
    pass_status = False  # 目錄輸入通過的狀態,初始不通過
    inp_limit = 5  # 輸入限制次數
    warn_str = "請輸入參考excel文件的完整路徑(如C:/Users/Default/Pictures/test.xls),退出請按’#‘:"
    for inp_count in range(inp_limit):
        pass_status = True  # 目錄輸入通過的狀態,布爾類型
        inp_path = input("文件路徑輸入限制{}次,第{}次\n".format(inp_limit, inp_count + 1) + warn_str)
        if '\\' in inp_path and '/' in inp_path:
            if inp_path == '#':
                exit()  # 退出程序
            print("輸入的文件夾路徑格式有誤,路徑不能同時用正斜槓‘/’和反斜槓'\\'表示\n")
            pass_status = False
        elif os.path.isdir(inp_path):  # 若輸入的是存在的文件夾路徑
            print("輸入的文件路徑格式有誤,路徑是文件夾路徑\n")
            pass_status = False
        elif os.path.isfile(inp_path):
            parent_path = os.path.dirname(inp_path)
            if parent_path == path:
                print("excel文件不能在要檢索的文件夾路徑中:{}\n".format(path))
                pass_status = False
                continue
        elif not os.path.exists(inp_path):  # 若輸入的是不存在的文件或文件夾
            print("文件路徑不存在")
            pass_status = False
        else:
            # 若是文件則要判斷,文件上級路徑是否存在
            print("輸入的文件路徑存在未知")
            pass_status = False
        if pass_status:
            return inp_path
    if not pass_status:
        print("{}次輸入excel文件完整路徑有誤,程序退出".format(inp_limit))
        exit()  # 路徑輸入錯誤,退出程序


# 打開excel文件
def open_excel(filename='example.xls'):
    try:
        excel = xlrd.open_workbook(filename)
        return excel
    except BaseException as e:
        print(e)


# 將浮點數據轉化整數數據
def convert_data(data_list):
    data_len = len(data_list)
    for i in range(data_len):
        data = data_list[i]
        if type(data).__name__ == 'float':      # type(data).__name__ 對象的類的名稱
            if data % 1 == 0:
                data_list[i] = int(data)        # xlrd 模塊會將Excel所有數字視爲浮點數,將數據轉爲整數
    return data_list


# 判斷參考excel文件、excel的sheet表、列名和要檢查的文件夾目錄是否存在
def exist_reference(path):
    excel_path = excel_directory(path)        # 參考excel文件的目錄
    excel = open_excel(excel_path)      # 打開excel文件
    sheet_list = excel.sheet_names()        # 獲取所有的sheet工作表名
    sheet_name = input("excel'{}'存在工作表{},請輸入參考工作表:".format(excel_path, sheet_list))
    if sheet_name not in sheet_list:        # 若指定工作表是不存在excel中,則退出程序
        print("excel'{}'不存在工作表{},程序退出".format(excel_path, sheet_name))
        exit()
    sheet = excel.sheet_by_name(sheet_name)  # 根據sheet名字來獲取excel中的sheet
    row_list = sheet.row_values(0)      # 獲取第一行的列名
    ref_col_name = input("excel'{}'指定工作表'{}'存在列名{},請輸入參考列名:".format(excel_path, sheet_name, row_list))
    # 判斷列名是否在工作表中
    if ref_col_name in row_list:  # 查找指定列名在第一行哪一列,即列索引
        col_index = row_list.index(ref_col_name)        # 指定列名在第一行的列索引
        column_list = sheet.col_values(col_index)       # 根據據指定列名在第一行的列索引查找目標列的數據
        column_list.pop(col_index)      # 根據第一行指定列名的索引,刪除列名在列表中的數據
        column_list = convert_data(column_list)    # 將指定列存在浮點數據轉爲整數數值
    else:
        print("excel'{}'的工作表{}不存在列名{},程序退出".format(excel_path, sheet_name, ref_col_name))
        exit()
    return column_list     # 返回要檢查的文件夾路徑,excel指定工作表指定列名的數據列表


# 去除文件後綴的文件名稱
def get_name_after_suffix(path):
    sep_str = '.jpg'       # 要分割的字符串標誌
    rm_suffix_list = []      # 去掉文件名稱的後綴名的名稱列表
    file_list = os.listdir(path)       # 獲取目錄的文件列表
    for file in file_list:
        if sep_str in file:
            name = file.split(sep_str)[0]
            rm_suffix_list.append(name)
    return rm_suffix_list


# 對比結果
def compare_file_column(check_list, refer_list):
    compare_list = []       # 對比列表
    for refer_name in refer_list:
        if refer_name not in check_list:
            compare_list.append(refer_name)
    return compare_list


if __name__ == '__main__':
    folder_dir = folder_directory("要檢查的")  # 要檢查的文件夾目錄
    name_list = get_name_after_suffix(folder_dir)       # 去除文件後綴的文件名稱
    res_list = exist_reference(folder_dir)     # 參考excel文件、參考sheet工作表、參考列名存在
    last_list = compare_file_column(name_list, res_list)        # 文件名和參考列名的對比結果
    print(last_list)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章