文件批量处理(一)

431 阅读1分钟

一、文件批量重命名

#  读取文件后,改文件名为“代码-部门简称.xls”
import xlrd
import xlwt
import os
import re
import sys


class MyFilter(object):
    def __init__(self, mylogfile=sys.stdout):
        self.f = mylogfile

    def write(self, data):
        if "WARNING *** OLE2 inconsistency" not in data:
            self.f.write(data)


# start up
log = open("the_log_file.txt", "w")
log_filter = MyFilter(log)


# book = xlrd.open_workbook("foo.xls", logfile=log_filter)

# 找到value里的code,先从title里找,如果没有,则从sheet_name里找
def find_file_code(title, sheet_name):
    reg = "[0-9a-zA-Z-_]+"
    match_value = re.search(reg, title)  # 在起始位置匹配
    if match_value:
        result = match_value.group()
        if re.search(r"[a-zA-Z]+", result):
            return result
    match_value = re.search(reg, sheet_name)
    if match_value:
        return match_value.group()
    return None


folder = 'C:\\Users\\雪小玲\\Desktop\\周末\\3、202106\\shiyan\\'
fixed_name = os.listdir(folder)
for department_name in fixed_name:
    work_path = folder + department_name + "\\"
    filenamelist = os.listdir(work_path)
    for i in filenamelist:
        # 如果是ZIP文件,则不处理
        if not i.endswith(".xls"):
            continue
        # 打开文件
        workbook = xlrd.open_workbook(work_path + i, formatting_info=True, logfile=log_filter)
        # 获取所有的sheet
        # print(workbook.sheet_names())
        # 根据sheet索引或者名称获取sheet内容
        sheet1 = workbook.sheet_by_index(0)
        print('sheet', i, sheet1.cell_value(0, 0))
        # print(sheet1)
        file_code = find_file_code(sheet1.cell_value(0, 0), workbook.sheet_names()[0])
        handled = False
        if file_code and not file_code.startswith("Sheet"):
            new_file_name = work_path + file_code + "-" + department_name + ".xls"
            new_file_name = new_file_name.replace("--", "-")
            if not os.path.exists(new_file_name):
                os.rename(work_path + i, new_file_name)
                handled = True
        else:
            print(department_name + work_path + i + "文件未处理")
        # print(new_name)

二、文件批量复制

import shutil,os
folder = 'C:\\Users\\雪小玲\\Desktop\\周末\\3、202106\\shiyan\\'
target = r'C:\Users\雪小玲\Desktop\周末\3、202106\test'
fixed_name = os.listdir(folder)
for department_name in fixed_name:
        source = folder+ department_name
        filenamelist = os.listdir(source)
        # print(os.listdir(source))
        for file in filenamelist:
            print(file)
            if file.endswith('.xls'):
                try:
                    shutil.copy(source+'\\'+file,target+"\\"+file)
                except IOError as e:
                    print("unable to copy file .%s" % e)