做了一个从Excel中筛选需要的列和行;并根据内容不同放到不同的Excel中的程序
导入需要的包 1 2 3 4 5 6 7 8 9 import sysimport xlrdimport xlwtfrom datetime import datetimefrom xlrd import xldate_as_tupleimport tkinterimport osfrom tkinter import filedialogfrom tkinter import messagebox
选取Excel文件并获取文件名和文件路径 1 2 3 4 5 6 from tkinter import filedialogtkinter.Tk().withdraw() Filepath = filedialog.askopenfilename(title=u'选择文件' ) Filename = os.path.basename(Filepath) if Filepath == '' : sys.exit()
读取Excel文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 '''读取工作表''' data = xlrd.open_workbook(Filepath) table = data.sheets()[0 ] '''总行数''' nrows = table.nrows '''总列数''' ncols = table.ncols '''获取第二行的列名称''' columnName = table.row_values(1 ) '''获取需要的列的字典''' saveColumn = ['任务号' , '物料编码' , '零件图号' , '数量' , '材质' , '尺寸' , '工序' , '完工日期' , '备料日期' , '成型日期' , '仓库' , '定额' , '工段' , '组件' , '外协' , '批次' ] columnNameDictionary = {} for i in range (ncols): if columnName[i] in saveColumn: columnNameDictionary[columnName[i]] = i '''获取需要的数据列数据,去掉不需要的列数据''' dataContent = [] for key, value in columnNameDictionary.items(): dataContent.append(table.col_values(value, start_rowx=1 , end_rowx=nrows)) '''去掉不需要的行数据,同时将列表存储数据的方式由列转为行''' ncols = len (dataContent) nrows = len (dataContent[0 ]) '''需要将材质列等于——的值整行删除''' material = 0 for i in range (nrows): if dataContent[i][0 ] == '材质' : material = i break finalDataContent = [] for i in range (nrows): temp = [] if dataContent[material][i] != '——' : for j in range (ncols): temp.append(dataContent[j][i]) finalDataContent.append(temp) temp = [] '''更新列名字典和行列数''' ncols = len (finalDataContent[0 ]) nrows = len (finalDataContent) columnNameDictionary.clear() for i in range (ncols): columnNameDictionary[finalDataContent[0 ][i]] = i
修改Excel内需要修改的数据 1 2 3 4 5 6 7 8 9 10 11 12 13 14 '''修改数据内容;按外协厂家进行分组-----为了减少循环次数放到了一块''' outsourcSet = set () for i in range (nrows): if i > 0 : finalDataContent[i][columnNameDictionary['零件图号' ]] = finalDataContent[i][columnNameDictionary['零件图号' ]].lower() finalDataContent[i][columnNameDictionary['零件图号' ]] = finalDataContent[i][columnNameDictionary['零件图号' ]].replace('.' , '-' ) finalDataContent[i][columnNameDictionary['零件图号' ]] = finalDataContent[i][columnNameDictionary['零件图号' ]].replace('艺' , 'y' ) finalDataContent[i][columnNameDictionary['数量' ]] = int (finalDataContent[i][columnNameDictionary['数量' ]]) finalDataContent[i][columnNameDictionary['完工日期' ]] = datetime(*xldate_as_tuple(finalDataContent[i][columnNameDictionary['完工日期' ]], 0 )).strftime('%Y-%m-%d' ) finalDataContent[i][columnNameDictionary['备料日期' ]] = datetime(*xldate_as_tuple(finalDataContent[i][columnNameDictionary['备料日期' ]], 0 )).strftime('%Y-%m-%d' ) finalDataContent[i][columnNameDictionary['成型日期' ]] = datetime(*xldate_as_tuple(finalDataContent[i][columnNameDictionary['成型日期' ]], 0 )).strftime('%Y-%m-%d' ) outsourcSet.add(finalDataContent[i][columnNameDictionary['外协' ]]) outsourc = list (outsourcSet)
str.lower()
是将字符串的字母全转为小写
str.replace('艺', 'y')
是将字符串的“艺”都转为“y”
finalDataContent[i][columnNameDictionary['完工日期']] = datetime(*xldate_as_tuple(finalDataContent[i][columnNameDictionary['完工日期']], 0)).strftime('%Y-%m-%d')
finalDataContent[i][columnNameDictionary['完工日期']]
的 值在读取过后被转为了数值,应该展示为日期,所以使用
datetime(*xldate_as_tuple(finalDataContent[i][columnNameDictionary['完工日期']], 0)).strftime('%Y-%m-%d')
来转为日期型并重新赋值给它
将data
由读出来的数值转为日期的方法为datetime(*xldate_as_tuple(data, 0)).strftime('%Y-%m-%d')
将数据存储为Excel文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 style = xlwt.XFStyle() al = xlwt.Alignment() al.horz = 0x02 al.vert = 0x01 style.alignment = al for i in range (len (outsourc)): name = Filepath.replace('★模板★' , '【' + outsourc[i] + '】' ) filename = xlwt.Workbook() sheet = filename.add_sheet(data.sheet_names()[0 ]) a = b = 0 for j in range (nrows): style2 = xlwt.easyxf('font:height 360;' ) row = sheet.row(j) row.set_style(style2) if j == 0 : for k in range (ncols): sheet.write(a, b, finalDataContent[j][k], style) b += 1 b = 0 a += 1 else : if finalDataContent[j][columnNameDictionary['外协' ]] == outsourc[i]: finalDataContent[j][columnNameDictionary['外协' ]] = '外协' + finalDataContent[j][columnNameDictionary['外协' ]] for k in range (ncols): sheet.write(a, b, finalDataContent[j][k], style) b += 1 b = 0 a += 1 '''设置每一列的宽度''' sheet.col(0 ).width = 256 * 14 sheet.col(1 ).width = 256 * 14 sheet.col(2 ).width = 256 * 20 sheet.col(3 ).width = 256 * 5 sheet.col(4 ).width = 256 * 10 sheet.col(5 ).width = 256 * 20 sheet.col(6 ).width = 256 * 20 sheet.col(7 ).width = 256 * 14 sheet.col(8 ).width = 256 * 14 sheet.col(9 ).width = 256 * 14 sheet.col(10 ).width = 256 * 10 sheet.col(11 ).width = 256 * 10 sheet.col(12 ).width = 256 * 14 sheet.col(13 ).width = 256 * 14 sheet.col(14 ).width = 256 * 14 sheet.col(15 ).width = 256 * 20 filename.save(name)
设置单元格数据居中的方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 style = xlwt.XFStyle() al = xlwt.Alignment() al.horz = 0x02 al.vert = 0x01 style.alignment = al sheet.write(a, b, finalDataContent[j][k], style)
设置单元格高度 1 2 3 style2 = xlwt.easyxf('font:height 360;' ) row = sheet.row(j) row.set_style(style2)
设置单元格高度 1 sheet.col(0 ).width = 256 * 14
将数据存储为txt文件 1 2 3 4 5 6 7 8 9 10 11 12 13 for i in range (len (outsourc)): name = Filepath.replace('★模板★.xls' , '【' + outsourc[i] + '】.txt' ) txtData = open (name, 'w' ) count = 0 for j in range (nrows): if j > 0 : if finalDataContent[j][columnNameDictionary['外协' ]] == '外协' + outsourc[i]: if count != 0 : print('' , file=txtData) count += 1 for k in range (ncols): print(finalDataContent[j][k], end='\t' , file=txtData) txtData.close()
完成后弹出提示框 1 messagebox.showinfo("提示", "文件分解完成")
打包文件 安装pyinstallerpip install pyinstaller
打包pyinstaller -F -w PlansToBreakUp.py
需要在文件在目录下运行
-w
是去掉运行时的黑框