python指定一列为KEY 合并多列单元格 并设置单元格行高

一、需求

1.第一列Sku数值相同的,合并A-E列,J-M列

2.M列设置行高完全显示内容

3.A1单元格内数字是SKU个数,合并单元格后数字更新

二、效果图

input入力文件处理前:

文件处理后 出力效果:

三、代码

1.合并单元格

 		wb = openpyxl.load_workbook('123.xlsx')
        name_list = wb.sheetnames
        for sheetName in name_list:
            mergedRowCount = 0  
            dw_sheet = wb[sheetName]
            startDataRow = 3  # 出力レポートのデータは3行目から書き込みが開始される
            needGroupAndMergeColumnsIndex = [1]  # 出力レポートの最初の3列にはがマージ必要となる
            for columnNumber in needGroupAndMergeColumnsIndex:
                #現在のセルの値
                currentValue = 0
                mergeValue = 0
                firstMergeRowNumber = 3
                #マージセルの個数
                addFlag = 0
                for rowNumber in range(startDataRow, dw_sheet.max_row+2):
                    mergeValue = currentValue
                    currentValue = dw_sheet.cell(
                        row=rowNumber, column=columnNumber).value

                    if currentValue == mergeValue:
                        addFlag += 1
                        if addFlag == 1:
                            firstMergeRowNumber = rowNumber - 1
                    else:
                        if addFlag == 0:
                            firstMergeRowNumber = rowNumber                            
                            mergedRowCount += 1 
                        else:
                            mergedRowCount += 1 
                            dw_sheet.merge_cells(
                                start_row=firstMergeRowNumber,
                                end_row=rowNumber-1,
                                start_column=columnNumber,
                                end_column=columnNumber)
                            dw_sheet.cell(row=firstMergeRowNumber,
                                         column= columnNumber).value = mergeValue
                            if columnNumber == 1:
#                                  
                                tempValueCol5 = 0
#  

                                if sheetName == name_list[0]:
                                    tempValueCol5 = dw_sheet.cell(
                                        row=firstMergeRowNumber, column=5).value
                                else:
                                    tempValueCol5 = dw_sheet.cell(
                                        row=firstMergeRowNumber, column=4).value * 0.8 - dw_sheet.cell(
                                        row=firstMergeRowNumber, column=4).value
                                
#
                                #セルをマージする
                                dw_sheet.merge_cells(
                                    start_row=firstMergeRowNumber,
                                    end_row=rowNumber-1,
                                    start_column=5,
                                    end_column=5)
                                dw_sheet.cell(row = firstMergeRowNumber,
                                              column= 5).value = tempValueCol5
            
                                for merge_col in [2,3,4,9,10,11,12,13]:
                                    dw_sheet.merge_cells(
                                    start_row=firstMergeRowNumber,
                                    end_row=rowNumber-1,
                                    start_column=merge_col,
                                    end_column=merge_col)
#                                
   
                            # セルのボーダーを描く
#                             if addFlag == 1:
#                                  for col in range(1,5):
#                                         dw_sheet.cell(row=firstMergeRowNumber+1,column=col).border=border
#                             else:
#                                 for row in range(firstMergeRowNumber+1,rowNumber):
#                                     for col in range(1,5):
#                                         dw_sheet.cell(row=row,column=col).border=border                       
                            addFlag = 0    
                if columnNumber == 1:
                    totalFormattedRowsCount = mergedRowCount - 1
                    raw_dw_txt = dw_sheet.cell(row=1, column=1).value
                    raw_dw_cnt = '(' + str(totalFormattedRowsCount)
                    dw_txt = raw_dw_txt + raw_dw_cnt
                    dw_sheet['A1'] = dw_txt
                        # wb.save('./template.xlsx')
            wb.save('123.xlsx')
            wb.close()

2.设置行高

wb = openpyxl.load_workbook('123.xlsx')
excelname='123.xlsx'
up_sheet = wb.sheetnames[0]
dw_sheet = wb.sheetnames[1]
up =  wb[up_sheet]
dw =  wb[dw_sheet]
up_df =pd.read_excel(io=excelname,sheet_name=up_sheet)
dw_df =pd.read_excel(io=excelname,sheet_name=dw_sheet)
# up_df = pd.read_excel(excelname, sheet_name=up_sheet)
# dw_df = pd.read_excel('input.xlsx', sheet_name=dw_sheet)
up_cnt = 3
for row in up_df.iterrows(): 
#     comment = 'V' + str(up_cnt)
    comment = 'M' + str(up_cnt)

#     up_sheet[comment]   = up_df.at[row[0], 'Unnamed: 21']
#     up_comment          = up_df.at[row[0], 'Unnamed: 21']
    up_comment          = up_df.at[row[0], 'Unnamed: 12']
    # 終わりの改行を削除します
#     up_comment = up_comment.strip("\n")
#     up_comment = up_comment.strip("\r")
   
    if isinstance(up_comment,float):
        up_comment = ''
        n_line_up_cnt = 0
         # 行の高さ設定   
    else:
        n_line_up_cnt = up_comment.count('\n')
  
        
    height_cnt = 1
    if up_comment.find('\n') > -1:
        height_cnt += n_line_up_cnt
        up_sen_list = up_comment.split('\n')
        for up_sen in up_sen_list:
            # 42.5文字を1行と設定する
            up_sen_len = len(up_sen) // 42.5
            height_cnt += up_sen_len
            up.row_dimensions[up_cnt].height = height_cnt * 19.2
    elif up_comment.find('[') > -1:
        # 42.5文字を1行と設定する
        up_sen_len = len(up_comment) // 42.5
        height_cnt += up_sen_len
        up.row_dimensions[up_cnt].height = height_cnt * 19.2
    up_cnt += 1
    wb.save('123.xlsx')

dw_cnt = 3
            
for row in dw_df.iterrows():
#     comment =   'V' + str(dw_cnt)
    comment =   'M' + str(dw_cnt)
#     dw_sheet[comment]   = dw_df.at[row[0], 'COMMENT']
#     dw_comment          = dw_df.at[row[0], 'Unnamed: 21']
    dw_comment          = dw_df.at[row[0], 'Unnamed: 12']
# 終わりの改行を削除します
#     dw_comment = dw_comment.strip("\n")
#     dw_comment = dw_comment.strip("\r")
   
    if isinstance(dw_comment,float):
        dw_comment = ''
        n_line_dw_cnt = 0
         # 行の高さ設定   
    else:
        n_line_dw_cnt = dw_comment.count('\n')
  
   
    height_cnt = 1
    if dw_comment.find('\n') > -1:
        height_cnt += n_line_dw_cnt
        dw_sen_list = dw_comment.split('\n')
        for dw_sen in dw_sen_list:
            # 42.5文字を1行と設定する
            dw_sen_len = len(dw_sen) // 42.5
            height_cnt += dw_sen_len
            dw.row_dimensions[dw_cnt].height = height_cnt * 19.2
    elif dw_comment.find('[') > -1:
        # 42.5文字を1行と設定する
        dw_sen_len = len(dw_comment) // 42.5
        height_cnt += dw_sen_len
        dw.row_dimensions[dw_cnt].height = height_cnt * 19.2
    dw_cnt += 1
    wb.save('123.xlsx')
    ```

你可能感兴趣的:(python_excel)