python使用openpyxl操作excel表格
2018-02-02 16:33
323 查看
#!Linux path:/usr/bin/env python3 # -*- coding: utf-8 -*- from openpyxl import Workbook from openpyxl import load_workbook from sys import argv from urllib.request import urlopen import os import warnings import sys inputdir = argv[1] #argv[1] #r"D:\8to9" #argv[1] 输入路径 outputdir = argv[2] #r"F:\pythonDemo\8to9\test\output" #argv[2] 输出路径 def split_file(): # 判断版本是否匹配 html = urlopen('***********') strl = str(html.read()) print(strl) if not '20171103' in strl: sys.exit(0) print("校验正确") # 获取映射文件和输出模版(和python文件同级) path = os.path.abspath('.') excel_files = os.listdir() tempExcels = {} map_excel = None for excel in excel_files: if not excel.startswith("input_8to9_") and not excel == "数据库列标题映射.xlsx": continue if excel == '数据库列标题映射.xlsx': template = path + '/' + excel print("映射文件路径:" + template) map_excel = load_workbook(filename=template) else: excelpath = path + '/' + excel # print("输出模版路径:" + excelpath) excelwork = load_workbook(filename=path + "/" + excel) tempExcels[excel] = excelwork excel_files1 = os.listdir(inputdir) for k in excel_files1: print(k) for excel in excel_files1: if excel == 'input_step4_计算输入文件.xlsx': print("loading file...") warnings.simplefilter("ignore") read_excel = load_workbook(filename = inputdir + "/" + excel) sheets = read_excel.get_sheet_names() sheet = read_excel.get_sheet_by_name(sheets[0]) mapsheet = map_excel.get_sheet_by_name("Sheet1") sheetNames = ['电力','工业锅炉','钢铁','平板玻璃','石油化工','焦化','其他建材','冶金','食品轻纺及其他','堆场','储罐','装载','泄漏','喷涂'] sheetAndColumnIn = {} # 输入文件的sheet和列 #{'电力':83,'工业锅炉':84} sheetAndColumnAll = {} # 输入文件的sheet和列 全部 sheetAndColumnMap = {} # 映射文件的sheet和列 #{"储罐": 3, "电力": 4} sheetAndRowDict = {} # 模版文件的sheet和行映射 headerMap = {} # 映射文件的标题映射 # 初始化sheetAndColumnIn for i in range(1,sheet.max_column+1): s = sheet.cell(row= 2,column=i).value if s in sheetNames: sheetAndColumnIn[s] = i # print(sheetAndColumnIn) # 初始化sheetAndColumnAll for i in range(1,sheet.max_column+1): s = sheet.cell(row= 1,column=i).value sheetAndColumnAll[s] = i # print(sheetAndColumnAll) # 初始化sheetAndRowDict for i in sheetNames: excel1 = tempExcels.get("input_8to9_"+ i + ".xlsx") sheets = excel1.get_sheet_names() sheet1 = excel1.get_sheet_by_name(sheets[0]) row = sheet1.max_row sheetAndRowDict[i] = row # # 初始化映射文件标题字典(中文为健) for i in range(1,mapsheet.max_column+1): s = mapsheet.cell(row= 1,column=i).value if s in sheetNames: sheetAndColumnMap[s] = i # print(sheetAndColumnMap) for i in sheetNames: temp = {} for j in range(2, mapsheet.max_row + 1): c = sheetAndColumnMap[i] k = mapsheet.cell(row=j,column=1).value v = mapsheet.cell(row=j, column=c).value if v : if k: temp[k]= v headerMap[i] = temp # 输出文件映射列 outWorkMap = {} for index in sheetNames: temp = {} excel2 = tempExcels.get("input_8to9_"+ index +".xlsx") sheets = excel2.get_sheet_names() sheet2 = excel2.get_sheet_by_name(sheets[0]) for i in range(1, sheet2.max_column+1): v = sheet2.cell(row=1,column=i).value if v: temp[v] = i outWorkMap[index] = temp # 遍历全部数据len(sheetNames)次,重复分配 print("insert data...") print(sheet.max_row) for i in sheetNames: temp = headerMap.get(i) # 原值:新值 list = temp.keys() # 原值 outMap = outWorkMap.get(i) # 新值:列 for l in range(3,sheet.max_row+1): colnum = sheetAndColumnIn.get(i) v = sheet.cell(row=l, column=colnum).value if v == '1' or v == 1: index = sheetAndRowDict[i] + 1 sheetAndRowDict[i] = index wb = tempExcels["input_8to9_"+ i +".xlsx"] sheets = wb.get_sheet_names() temp_sheet = wb.get_sheet_by_name(sheets[0]) # 插入一条数据 for m in list: colnum1 = sheetAndColumnAll.get(m) if colnum1 == None: # print("映射文件和input文件"+ i + m + "字段不匹配") continue v = sheet.cell(row = l,column= colnum1).value row = sheetAndRowDict.get(i) # 通过m获取模版表中的标题,通过标题获取列号 colnum2 = outMap.get(temp.get(m)) if colnum2 == None: # print( "映射文件和模版文件"+ i + m + "字段不匹配") continue temp_sheet.cell(row = row,column = colnum2,value = v) print(i + " 数据插入成功") print("save file...") for index in sheetNames: tempExcels["input_8to9_"+ index +".xlsx"].save(outputdir + "/" + "output_8to9" + '_' + index + '.xlsx') print("succefully!") return 0 if __name__ == "__main__": split_file()
相关文章推荐
- python3使用 openpyxl 操作excel表格
- python使用openpyxl库修改excel表格数据
- python 使用openpyxl来写数据到excel表格
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- 使用Python处理excel表格(openpyxl)及表格中的中文处理
- Python操作Excel表格,xlwt模块的使用
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- python模块(使用xlrd、xlwt操作excel表格详解)
- python使用easy_install安装支持excel2007操作的openpyxl
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- [python]使用xlrd对Excel表格进行读写操作
- python中使用xlrd、xlwt操作excel表格详解
- [python]使用xlrd对Excel表格进行读写操作
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解
- python中使用xlrd、xlwt操作excel表格详解