유 니 버 설 만능 엑셀 가 져 온 Python 구현

17744 단어
유 니 버 설 만능 엑셀 가 져 온 Python 구현
정 언
        디자인 방안 부터 알고리즘 과 기능 코드 실현 까지 일주일 이 걸 렸 습 니 다. 여기 서 기록 하면 여러분 이 참고 할 수 있 습 니 다.쓸데없는 소리 하지 마 세 요. 우선 설정 파일 을 통 해 원 하 는 데 이 터 를 설정 하고 간단 한 표현 식 만 설정 하면 가치 있 는 데이터 분석 과 가치 있 는 정보 데 이 터 를 발굴 할 수 있 습 니 다.최종 적 으로 얻 은 것 은 필드 를 비롯 한 제 이 슨 데이터 다.후기 에는 엑셀 이나 내 보 내기 기능 을 시각 화하 여 사용자 의 조작 과 체험 을 더욱 편리 하 게 할 수 있다.
프로필      
{
	"updateTime": "2018-02-07",           
	"database_host": "10.10.10.6",         
	"database_port": 9400,                
	"database_name": "mdap-dev",    
	"filePath":"/projectName/input", 
	"fileName":"test.xlsx", 	      
	"$excelConfig": [  					  
		{
			"sheetName":"    " ,  	  
			"target_data_area":"A2~A31",
			"target_data_key":"(x=A,y=1)",
			"datas":[ 					
			 		{
						"value": "(x=B,y=_y)",
						"key":"    "
					},
					{
						"value": "(x=C,y=_y)",
						"key":"(x=C,y=1)"
					},
					{
						"value": "(x=D,y=_y)",
						"key":"(x=D,y=1)"
					},
					{
						"value": "(x=E,y=_y)",
						"key":"(x=E,y=1)"
					},
					{
						"value": "(x=F,y=_y)",
						"key":"(x=F,y=1)"
					}
				
			 ]
			
		}
		
	]
}

셋.프로필 설명              
"updateTime":       
"database_host": "10.10.10.6",               
"database_port": 9400,                  
"database_name": "mdap-dev",      ,  
"filePath":"/projectName/input",         
"fileName":"test.xlsx", 	      	       
"$excelConfig": [  			                           
	"sheetName":"    " ,  	     sheet    
	"target_data_area":"A2~A31",              
	"target_data_key":"(x=A,y=1)",       
	"datas":[ 					       
				"value": "(x=B,y=_y)",         (                 )
				"key":"    " 		    "    "

4. python 코드
       4.1 test.py
        
#coding=utf-8
import json
import config
import os, configparser
from importExcel import commonExcelImport 
'''
       excel    json    
    
@author: andong

'''

#def loadSettingFile():

cp = configparser.ConfigParser()
Settings = dict(
    config_file = os.path.join(os.path.dirname(__file__),'template.json'),
    config = cp
)

path=r"D:\workSpace\allExcelExport\config\template.json"
with open(path, "r", encoding='UTF-8') as excelTemplate:
    data = json.load(excelTemplate)
    updateTime = data['updateTime']
    filePath = data['filePath']
    fileName = data['fileName']
    excelConfig = data['$excelConfig']
    counter = 1;
    for i in range(len(excelConfig)):#         sheet
        print ("  :%s    :%s" % (i + 1, excelConfig[i]))
        if __name__ == '__main__':
            commonExcelImport(filePath,fileName,excelConfig[i])
            
       # result = importExcel.ready_excel(filePath,fileName,excelConfig[i])
        





4.2 importExcel.py
    
import config,logging
import xlrd,xlwt
import uuid,json,re
import openpyxl,string
from openpyxl.cell import Cell
from openpyxl.workbook import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.utils import column_index_from_string
from warnings import catch_warnings
# pyexcel_xls   OrderedDict         
#from collections import OrderedDict  
  
class MyException(Exception):
    def __init__(self,message):
        Exception.__init__(self)
        self.message=message
        
class commonExcelImport(object):
    def __init__(self,filePath,fileName,config):
        self.filePath=filePath
        self.fileName=fileName
        self.config=config
        self.ready_excel(filePath,fileName,config)
    #coding=utf-8
    '''
           excel    json    
        
    @author: andong
    
    '''
    @classmethod
    def changePath(path):
        if("\\\\" in path):
            pass
        elif("\\" in path):
            path = path.replace("\\", "//") 
        return path 
    
    
    '''
        
    '''
    @classmethod
    def ready_excel(self,filePath=None,fileName=None,config={}):
        file_path=r"D:\workSpace\allExcelExport\input\        .xlsx"
        wb=openpyxl.load_workbook(file_path)  #  excel  
        sheetName = config["sheetName"]
        if (sheetName==""):
            ws=wb.active
        else:
            ws=wb[sheetName]
        #   
        target_data_area = config["target_data_area"]
        target_data_key = config["target_data_key"]
        all_sheets_name = wb.get_sheet_names() #           
        sheet=wb.get_sheet_by_name(sheetName)  #     
        if target_data_key.startswith("(") and target_data_key.endswith(")"):
            target_data_key=target_data_key.lstrip('(')
            target_data_key =target_data_key.rstrip(')')
            target_data_key = target_data_key.split(",")[0].split("=")[1] + target_data_key.split(",")[1].split("=")[1]
            target_data_key =sheet[target_data_key].value
        logging.info("      key="+target_data_key)
        value = config["datas"] #       
        sheet02=wb.get_active_sheet()  #        
        d1 = target_data_area.split("~")[0].split("\\.")[0]
        d2 = target_data_area.split("~")[1]
        rownum_start=sheet[d1].row
        rownum_end = sheet[d2].row
        row_num = rownum_end - rownum_start +1
        colnum_start = sheet[d1].column
        colnum_end = sheet[d2].column
        dateArea_num = self.letter_differ(colnum_start,colnum_end) #    
        wb=openpyxl.Workbook()
        wbActiveService=wb.active
    #     cell_range=wbActiveService['A1':'C2']
    #     print(sheet.rows)
    #     print(len(cell_range))
    #     for cell in cell_range:
    #         print(cell)
    #     #  
    #     print(sheet['B1'].value)  #      
    #     print(sheet['B1'].column)  #       
    #     print(sheet['B1'].row)  #       
    #     print(sheet.cell(row=8,column=1).value)  #      ,column row    
    #     for i in range(1,4,1):
    #         print(sheet.cell(row=i,column=2).value) #      
    #     print(sheet.max_column)  #      
    #     print(sheet.max_row)  #      
        try:
            result_excel ={}
            target_data_area_value=[]
            target_data_area_value.append(ws[d1:d2])   
            result_excel.update({target_data_key:target_data_area_value})
            if value:
                for index,data in enumerate(value):
                    logging.info(data)
                    logging.info(data["key"])#  key
                    if data["key"].startswith("(") and data["key"].endswith(")"):
                        str=data["key"].lstrip('(')
                        str =data["key"].rstrip(')')
                        data_key = str.split(",")[0].split("=")[1] + str.split(",")[1].split("=")[1]
                        data_key =sheet[data_key].value
                    else:
                        data_key = data["key"]
                    result = self.parse_excel_data(file_path,sheetName,target_data_key,row_num,colnum_start,colnum_end,rownum_start,rownum_end,data_key,data,dateArea_num)
                    result_excel.update(result)
            else:
                pass
            logging.info("result_excel===>",result_excel)
            result = self.findDataFromExcel(file_path,sheetName,result_excel)
            resultJson = self.parse_need_datas(result) #      
            return resultJson
        except (KeyboardInterrupt, SystemExit):
            logging.error("    ")
        finally:
            result_excel.clear()
            del target_data_area_value[:]
    
    @classmethod
    def parse_excel_data(self,fileAddr=None,sheetName=None,relative_key=None,row_num=None,letter_1=None,letter_2=None,number_1=None,number_2=None,key=None,value=None,span=None):
        list_value=[]
        result={}
        wb=openpyxl.load_workbook(filename=fileAddr,read_only=True)
        if (sheetName==""):
            ws=wb.active
        else:
            ws=wb[sheetName]
        data = value['value']
        sheet=wb.get_sheet_by_name(sheetName)  #     
        #wb=openpyxl.Workbook()
        #ws=wb.active
        print( data.split(",")[0].split("(")[1])
        print(data.split(",")[1].split(")")[0])
        try:
            #if data.split(",")[0].split("(")[1].split("=")[1].find("_") !=-1 :
            x_num = data.split(",")[0].split("(")[1].split("=")[1].strip()
            #if data.split(",")[1].split(")")[0].split("=")[1].find("_") !=-1 :
            y_num = data.split(",")[1].split(")")[0].split("=")[1].strip()
            #TODO
            if data.split(",")[0].split("(")[1].split("=")[1].find("_") !=-1 and data.split(",")[1].split(")")[0].split("=")[1].find("_") ==-1: #x  
                x_start_num = column_index_from_string(letter_1)
                x_end_num = column_index_from_string(letter_2)+1
                for num in range(row_num):
                    for start_num in range(x_start_num,x_end_num):
                        x_change_letter = get_column_letter(start_num)
                        list_value.append(x_change_letter+str(y_num))
                #for r in range(row_num):
                #    list_value.append(ws[startPosition:endPosition])
            elif data.split(",")[1].split(")")[0].split("=")[1].find("_") !=-1 and data.split(",")[0].split("(")[1].split("=")[1].find("_") ==-1:#y  
                for num in range(number_1,number_2+1):
                    for letter_num in range(span):
                        list_value.append(x_num + str(num))
                        
            elif data.split(",")[0].split("(")[1].split("=")[1].find("_") !=-1 and data.split(",")[1].split(")")[0].split("=")[1].find("_") !=-1 : #x,y  
                xystart_letter_befor = letter_1
                xystart_letter_later = number_1
                xyend_letter_befor = letter_2
                xyend_letter_later = number_2
                start = xystart_letter_befor+str(xystart_letter_later)
                end = xyend_letter_befor+str(xyend_letter_later)
                xy_col = self.letter_differ(xystart_letter_befor,xyend_letter_befor)
                xy_row = int(xyend_letter_later) - int(xystart_letter_later) +1
        #         for col in range(xy_col):
        #             for row in range(xy_row):
        #                 list_value.append(ws[start:end])
                list_value.append(ws[start:end])
            else : # x,y  
                val = x_num +y_num
                for x in range(span):
                    for y in range(number_2-number_1+1):
                        list_value.append(val)
                
            jsondata={}
            jsondata[key] = list_value
            jsondata.update({key:list_value})
            return jsondata
        except Exception:
            raise MyException("  excel    !")
    
     
     
    '''
         excel     
                  
    '''
    @classmethod
    def findDataFromExcel(self,fileAddr=None,sheetName=None,jsondata={}):
        json_result={}
        #sheetContent.get_highest_row() #sheetContent.get_highest_row()
        for index,value in enumerate(jsondata):
            print(jsondata[value])
            result = self.parseExcelGetDatas(value,fileAddr,sheetName,jsondata[value])
            json_result.update(result)
        #    excel
    #     for row in ws.rows:
    #         list=[] 
    #         #    excel     
    #         for cell in row:
    #             val=str(cell.value)
    #             print(val)
        logging.info(json_result)
        return json_result
    
    '''
        excel    
    '''
    @classmethod
    def parseExcelGetDatas(self,value=None,fileAddr=None,sheetName=None,jsondata=None):
        wb=openpyxl.load_workbook(filename=fileAddr,read_only=True)
        if (sheetName==""):
            ws=wb.active
        else:
            ws=wb[sheetName]
        sheet=wb.get_sheet_by_name(sheetName)  #     
        resultJson=[]
        result={}
        workbook = xlrd.open_workbook(fileAddr)
        sheet1 = workbook.sheet_by_name(sheetName)
        merged_cell = sheet1.merged_cells
        for i in range(len(jsondata)):
            if isinstance(jsondata[i], (tuple)) :
                jsonData=[]
                for y in jsondata[i]:
                    for ii in y:
                        print(str(ii).split(".")[1].rstrip(">"))
                        print(sheet[str(ii).split(".")[1].rstrip(">")].value)
                        deal_data = str(ii).split(".")[1].rstrip(">")
                        for ml in range(len(merged_cell)):
                            merged_cell_row1 = merged_cell[ml][0]
                            merged_cell_row2 = merged_cell[ml][1]
                            merged_cell_col1 = merged_cell[ml][2]
                            merged_cell_col2 = merged_cell[ml][3]
                            re_number_data = int(re.sub("\D", "", deal_data))#    
                            re_letter = re.sub("\d", "", deal_data)#    
                            re_letter_data = column_index_from_string(re_letter)
                            if (merged_cell_col1 < re_letter_data and re_letter_data <= merged_cell_col2) and (re_number_data > merged_cell_row1 and re_number_data <= merged_cell_row2):
                                needCell_data = sheet1.cell_value(merged_cell_row1,merged_cell_col1)
                                resultJson.append(needCell_data)
                                break
                            elif ml < len(merged_cell)-1:
                                continue
                            else:
                                resultJson.append(sheet[deal_data].value)
            else:
                for ml in range(len(merged_cell)):
                    merged_cell_row1 = merged_cell[ml][0]
                    merged_cell_row2 = merged_cell[ml][1]
                    merged_cell_col1 = merged_cell[ml][2]
                    merged_cell_col2 = merged_cell[ml][3]
                    re_number_data = int(re.sub("\D", "", jsondata[i])) #    
                    re_letter = re.sub("\d", "", jsondata[i])#    
                    re_letter_data = column_index_from_string(re_letter)
                    if (merged_cell_col1 < re_letter_data and re_letter_data <= merged_cell_col2) and (re_number_data > merged_cell_row1 and re_number_data <= merged_cell_row2):
                        needCell_data = sheet1.cell_value(merged_cell_row1,merged_cell_col1)
                        resultJson.append(needCell_data)
                        break 
                    elif ml < len(merged_cell)-1:
                        continue
                    else:
                        resultJson.append(sheet[jsondata[i]].value)
        result[value] = resultJson
        result.update({value:resultJson})
        #result.update({value:resultJson})
        return result
    '''
    dict      json   
    
    '''
    
    
    
    '''
           
    '''
    @classmethod
    def parse_need_datas(self,jsonData=None):
        key_data=[]
        result=[]
        key_name=[]
        #     logger    
        logger = logging.getLogger()  
        for key in jsonData:
           key_data.append(key)
           key_name.append(key)
        result.append(key_name)
        for i in range(len(jsonData[key_data[0]])):
            resultJson=[]
            for j in jsonData:
               resultJson.append(jsonData[j][i]) 
            result.append(resultJson)
            #del resultJson[0:]
        logger.info(result)
        print(result)
        return result
       
    '''
    excel   
    @param obj2dData
    A~ZZZ
          
    '''
    @classmethod
    def letter_differ(self,start=None,end=None):
        X = []  
        Y = []  
        for i in range(26):  
            X.append(i+1)  
            Y.append(chr(65+i))
        for i in range(26):  
            for j in range(26): 
                 Y.append(chr(65+i)+chr(65+j))        
        for i in range(26):  
            for j in range(26): 
                for k in range(26): 
                    Y.append(chr(65+i)+chr(65+j)+chr(65+k))  
                 
        for k in range(26*26*26): 
            if k > 26:
                X.append(k)
        #print(Y)  
        result = Y.index(end)-Y.index(start) +1
        return result



6 인 것 같 아 요. 많이 사랑 해 주세요!!

좋은 웹페이지 즐겨찾기