""" 现在的需求变了。文件列表中的文件名,也就是项目名,统一放到第一列,而且放到总表里的相同项目的行,第一列的项目名要合并单元格需求又变了,因为合并单元格会干扰筛选,所以只能每一列都加名字"""import os,globimport win32com.client import win32apiimport re,timeimport ioimport syssys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')#这三行为了让终端显示中文def getFilenameWithoutSuffix(path): filename = os.path.basename(path) filename = filename.split('.')[1] #文件名是22.xxxx.xlsx return filenamedef matchStr(input_str,ref_str): if type(input_str) == str: match = re.search(ref_str,input_str) if match: return True else: return False else: return Falsedef checkNewTable(sheet,rowNum,colNum): for row in range(1,rowNum+1): for col in range(1,colNum+1): if matchStr(sheet.Cells(row,col).Value,'设备招标情况'): return [True,row,col] elif matchStr(sheet.Cells(row,col).Value,'采购状态'): return [False,row,col] print('\033[1;35m 查询不到表头! \033[0m') return [0,0,0]def strProcess(input_str): input_str = input_str.replace('$','')#去除$号 b=re.sub('[A-Za-z]','',input_str)#去除字母 return b#从"2:4"这样的字符串中获取差值def getDifNumber(input_str): num_list = input_str.split(':') dif_num = int(num_list[1]) - int(num_list[0]) return dif_numdef pasteRows(sheet,row,paste): paste_range = 'B' + str(row) sheet.Range(paste_range).PasteSpecial(Paste=paste)#要带列宽的话需要多执行一遍Paste=8def getCellAddress(sheet,row,col): temp_address = sheet.Cells(row,col).Address cell_address = temp_address.replace('$','') return cell_addressdef clearCacheInFilelist(filelist): #去除glob查到的~前缀的缓存文件 for i in range(len(filelist)-1,-1,-1): match = re.search('~',filelist[i]) if match: print(filelist[i]) filelist.remove(filelist[i]) return filelistdef excludeOtherValues(input_str): if type(input_str) == str: match = re.search('已签|已定标|现场零星采购',input_str) if match: return True else: return False else: return Falsefilelist = glob.glob(r'文件夹\*.xlsx')file_dir = clearCacheInFilelist(filelist)pos_col = 3 #以经办人这一列来确定是否为有效行sht2_row = 4 #开始复制的行color = int('E8E8E8',16)list1 = []first_num = 0trigger = Falsetry: xlApp = win32com.client.Dispatch('Excel.Application') xlApp.Visible = False xlBook2 = xlApp.Workbooks.Open(r'E:\测试\测试.xlsx') sht2 = xlBook2.Sheets(1) colNum2 = sht2.UsedRange.Columns.Count for i in range(len(file_dir)): xlBook1 = xlApp.Workbooks.Open(Filename=file_dir[i],ReadOnly=True) filenameWithoutSuffix = getFilenameWithoutSuffix(file_dir[i]) print('\033[1;32;43m ',filenameWithoutSuffix,' \033[0m!') sht1 = xlBook1.Sheets(1) #默认选择第一张表 rowNum = sht1.UsedRange.Rows.Count #返回文件1的行数 colNum = sht1.UsedRange.Columns.Count #返回文件1的列数 cell_state = checkNewTable(sht1,rowNum,colNum) header_row = cell_state[1] #如果表头是合并单元格,也没必要加一 match_col = cell_state[2] #匹配列的列数 if cell_state[0]==True: for row in range(header_row+1,rowNum+1): pos_cell = sht1.Cells(row,pos_col) cell = sht1.Cells(row,match_col) print('第',row,'行:',end=' ') sys.stdout.flush() #刷新缓存区 if type(pos_cell.Value) == str: if pos_cell.MergeCells : temp_address = sht1.Cells(row,pos_col).MergeArea.Address#获取合并单元格的地址 rows_range = strProcess(temp_address)#清洗得到的字符串 print('合并单元格行数--',rows_range) sys.stdout.flush() temp_range = rows_range.split(':') copy_list = ['A',str(temp_range[0]),':','AJ',str(temp_range[1])] copy_area = ''.join(copy_list) dif_num = getDifNumber(rows_range) if excludeOtherValues(cell.Value) == False : if first_num == 0: first_num = sht2_row trigger = True print(cell.Value) sht1.Range(copy_area).Copy() #复制这几行 pasteRows(sht2,sht2_row,14) #粘贴到总表 cell_address = getCellAddress(sht2,sht2_row,1) #这两行在表末尾增加了一列,给单元格赋值,内容是表名称 #last_cell = getCellAddress(sht2,sht2_row + dif_num,colNum+1) last_cell = getCellAddress(sht2,sht2_row + dif_num,1) #获取首末单元格地址 merge_range = cell_address + ':' + last_cell sht2.Range(merge_range).Value = filenameWithoutSuffix print('复制到表2第',sht2_row,'行',end='***') sht2_row += dif_num + 1 print('下次行数:',sht2_row) sys.stdout.flush() else: print('\033[1;35m 非空标准单元格! \033[0m',pos_cell.Value) if matchStr(cell.Value,'合同已签')==False : trigger = True sht1.Rows(row).Copy()#复制这几行 pasteRows(sht2,sht2_row,14) sht2_row += 1 sys.stdout.flush() #刷新缓存区 if trigger == True: last_num = sht2_row - 1 copy_rows = str(first_num) + ':' + str(last_num) first_num = last_num + 1 list1.append(copy_rows) trigger = False sht2.Cells.FormatConditions.Delete()#清除条件格式,不然格子的颜色会乱掉 for i in range(len(list1)): if i % 2 == 0: sht2.Range(list1[i]).Interior.Color = color xlApp.CutCopyMode = False xlBook1.Close(False) xlBook2.Save() sht2.PageSetup.PrintTitleRows = "$2:$3"#设置打印顶端标题(即每一页都要打印23行) # sht2.PageSetup.PrintTitleColumns = "$A:$B"#设置重复打印列 # sht2.PageSetup.PrintTitleColumns = ""#取消设置 # sht2.PageSetup.PrintTitleColumns = False #取消设置finally: # if xlApp.ClipboardFormats[0]: # xlApp.CutCopyMode = False#清空剪贴板 xlApp.quit() print(list1)