python--文件对比,整理,合并excel脚本

描述

  • 有许多文件夹,里面可能有成百上千个文件
  • 内容主要分为sql文件 和 excel文件
  • excel文件记录sql文件信息,同时是sql文件的描述

需求

  • 由于许多人提交不规范 找出sql文件和excel文件记录信息有差异的地方,主要是名称
  • 将sql文件从这些文件夹中汇总,加上数字前缀,同时若为特定脚本,在汇总文件夹中存放时,需要另建文件夹加以区分
  • 将所有excel汇总,同时将不规范格式的excel进行处理汇总

1 sql文件和excel信息比对脚本

#!/usr/bin/python  
# -*- coding:utf8 -*- 
import os
import openpyxl
from openpyxl import Workbook,load_workbook
sql_list=[]
excel_list=[]
for root,dirs,files in os.walk(r"."):
    for file in files:
        this_file=os.path.join(root,file)
        if ".sql" in this_file or ".dmp" in this_file:
            index=file.find('.')
            sql_list.append(file[:index])
        if ".xl" in this_file and "~" not in this_file:
            excel_list.append(this_file)
sql_list_in_excel=[]
for excel_name in excel_list:
    wb = load_workbook(excel_name)
    #ws = wb.get_sheet_by_name(wb.sheetnames[0])
    ws=wb.worksheets[0]
    rows=ws.max_row
    cols=ws.max_column
    col_number=5
    for i in range(2,cols+1):
        val=ws.cell(row=1,column=i).value
        if val and '脚本' in val:
            col_number=i
            break
    for i in range(2,rows+1):
        val=ws.cell(row=i,column=col_number).value
        if val and 'None' not in val:
            index=val.find('.')
            if index != -1:
                sql_list_in_excel.append(val[:index])
            else:
                sql_list_in_excel.append(val)
    wb.close()
#print(val[:index])
print("-----------sql文件比excel中多-----------")
for this_sql in sql_list:
    if this_sql not in sql_list_in_excel:
        print(this_sql)
print("-----------excel比sql文件多-------------")
for this_excel in sql_list_in_excel:
    if this_excel not in sql_list:
        print(this_excel)
#pyinstaller打包后,防止闪退
wait=input()

2 汇总文件脚本

#!/usr/bin/python
# -*- coding:utf8 -*-
import os
import shutil
print('just waiting...')
if os.path.exists('scripts'):
    shutil.rmtree('scripts')
os.mkdir('scripts')
if os.path.exists('excels'):
    shutil.rmtree('excels')
os.mkdir('excels')
i=0
j=0
for root,dirs,files in os.walk(r'.'):
    dirs[:] = [d for d in dirs if d not in "scripts"]
    dirs[:] = [d for d in dirs if d not in "excels"]
    for f in files:
        f_name=os.path.join(root,f)
        index=f_name.rfind('\\')
        this_dir=f_name[:index]
        index=this_dir.rfind('\\')
        this_dir=this_dir[index+1:]
        #print(this_dir)
        if '.sql' in f_name or '.dmp' in f_name:
            if '仅' in f_name or '执行' in f_name:
                this_path='scripts'+'/'+this_dir
                if not os.path.exists(this_path):
                    os.mkdir(this_path)
                i=i+1
                tar_file_name=str(i)+'_'+f
                f_tar=this_path+'/'+tar_file_name
                shutil.copyfile(f_name,f_tar)
            else:
                i=i+1
                tar_file_name=str(i)+'_'+f
                f_tar='scripts/'+tar_file_name
                shutil.copyfile(f_name,f_tar)
        if '.xl' in f_name and '~' not in f_name:
            j=j+1
            tar_file_name=str(j)+'_'+f
            f_tar='excels/'+tar_file_name
            shutil.copyfile(f_name,f_tar)

3 按格式合并excel脚本

#!/usr/bin/python
# -*- coding: utf8 -*-
import os
import openpyxl
from openpyxl import load_workbook
def format_date(this_date):
    this_date=str(this_date)
    this_date=this_date.rstrip()
    this_len=len(this_date)
    #print(this_len)
    if this_len==19:
        return(this_date[:10])
    if this_len==10:
        return(this_date[0:4]+'-'+this_date[5:7]+'-'+this_date[8:])
    if this_len==8:
        return(this_date[0:4]+'-'+this_date[4:6]+'-'+this_date[6:])
    else:
        return(this_date)
if os.path.exists('all_xl.xlsx'):
    os.remove('all_xl.xlsx')
#存放excel文件的目录为./excels
excel_list=[]
for root,dirs,files in os.walk(r"./excels"):
    excel_list[:]=files
#print(excel_list)

s_wb = openpyxl.Workbook()
s_ws = s_wb.active
s_ws.append(['主体域','对象列表','表名','对象修改类型','脚本','变更内容','发起人','修改时间','脚本类型','小组审核人'])
for this_excel in excel_list:
    xl_file_name="./excels/"+this_excel
    wb = load_workbook(xl_file_name)
    ws=wb.worksheets[0]
    #print(ws.merged_cells)
    #resolve irregularities
    if ws.max_column == 14:
        for row in range(2,ws.max_row+1):
            val_row_list=[]
            none_count=0
            for i in range(1,ws.max_column+1):
                if str(ws.cell(row,i).value) == 'None':
                    none_count+=1
            if none_count == 14:
                break
            val_row_list.append(ws.cell(row,1).value)
            val_row_list.append('表')
            val_row_list.append(ws.cell(row,2).value)
            val_row_list.append(ws.cell(row,4).value)
            val_row_list.append(ws.cell(row,3).value)
            val_row_list.append(ws.cell(row,4).value)
            val_row_list.append(ws.cell(row,5).value)
            val_row_list.append(ws.cell(row,8).value)
            val_row_list.append(ws.cell(row,9).value)
            val_row_list.append(ws.cell(row,11).value)
            #print(val_row_list)
            s_ws.append(val_row_list)
        wb.close()
    else:
        for row in range(2,ws.max_row+1):
            val_row_list=[]
            none_count=0
            for i in range(1,ws.max_column+1):
                if str(ws.cell(row,i).value) == 'None':
                    none_count+=1
            if none_count == 10:
                break
            for col in range(1,ws.max_column+1):
                this_val=str(ws.cell(row,col).value)
                #print(this_val)
                if "None" == str(this_val):
                    number=row
                    while "None" == str(this_val) and number>1:
                        number=number-1
                        this_val=str(ws.cell(int(number),col).value) 
                    val_row_list.append(this_val)
                else:
                    val_row_list.append(this_val)
            s_ws.append(val_row_list)
        wb.close()
#start merging
rows=['']+list(s_ws.rows)  
index=2
row_count=len(rows)
while index < row_count:
    value=rows[index][4].value
    for this_index,row in enumerate(rows[index+1:],index+1):
        if not (row[4].value==None or row[4].value==value):
            break
    else:
        s_ws.merge_cells('E'+str(index)+':E'+str(this_index))
        break
    s_ws.merge_cells('E'+str(index)+':E'+str(this_index-1))
    index=this_index
for row in range(2,s_ws.max_row+1):
    value_addr='H'+str(row)
    s_ws[value_addr]=format_date(s_ws[value_addr].value)
s_wb.save('all_xl.xlsx')

说明

  • 使用pyinstaller打包成exe文件
  • 脚本1单独打包 脚本2和3一起打包
  • 打包时候确保有相关库: openpyxl


    workspace.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容