KJ9 KJ9 - 2 years ago 100
Python Question

How can merge same data cell?

I made some script for data modify and write xlsx.

My text file has below data.

WWN,Server Name,Server IP,SP port1,Storage Group1,SP port2,Storage Group2,LUN Count,LUN Capacity
8A:34,11B2,14,SP A_4,11B2,SP B_4,11B2,13,133
8A:36,11B2,14,SP A_8,11B2,SP B_8,11B2,13,133
8A:38,11B6,15,SP A_4,11B6,SP B_4,11B6,13,133
8A:3A,11B6,15,SP A_8,11B6,SP B_8,11B6,13,133

I want same data cell merged using key data.
Key data is
Storage Group1
Storage Group1
cell has same data, will be merge
LUN count
LUN Capacity

below is my script not work.

import sys
import pandas as pd
import xlsxwriter

##Add workbook and worksheet
workbook = xlsxwriter.Workbook('emc_pool.xlsx', {'strings_to_numbers': True})
worksheet = workbook.add_worksheet(u'Host')

##Add Cell Style
bold = workbook.add_format({'bold': True, 'border': 1})
border = workbook.add_format({'border': 1})

merge_format = workbook.add_format({'bold': True, 'border': 1,'align': 'center', 'valign': 'vcenter'})

##aliase information
port_file = 'last_emc.txt'
port_table = pd.read_table(port_file, encoding='utf_8', sep=',', header=None, names=['WWN','Server Name','Server IP','SP port1','Storage Group1','SP port2','Storage Group2','LUN Count','LUN Capacity'], lineterminator='\n')

worksheet.merge_range('A1:G1', u'Host', merge_format)
worksheet.write('A2', 'Server Name', bold)
worksheet.write('B2', 'Server IP', bold)
worksheet.write('C2', 'WWN', bold)
worksheet.write('D2', 'SP port1', bold)
worksheet.write('E2', 'Storage Group1', bold)
worksheet.write('F2', 'SP port2', bold)
worksheet.write('G2', 'Storage Group2', bold)
worksheet.write('H2', 'LUN count', bold)
worksheet.write('I2', 'LUN Capacity', bold)

data = (port_table)

row = 2
col = 0

data_dict = {}
count = 0
for index, temp_data in data.iterrows():
data_line = data_dict.setdefault(temp_data[4], [0])
data_line[0] += (count + 1)
for key, data in data_dict.items():
if index != 0:
worksheet.write(row, col, temp_data[1], border)
worksheet.write(row, col + 1, temp_data[2], border)
worksheet.write(row, col + 2, temp_data[0], border)
worksheet.write(row, col + 3, temp_data[3], border)
for temp_data[4] in [key]:
worksheet.merge_range(int(row) + map(int,data), col + 7, temp_data[7], border)
worksheet.merge_range(int(row) + map(int,data), col + 8, temp_data[8], border)
worksheet.merge_range(row, col + 7, temp_data[7], border)
worksheet.merge_range(row, col + 8, temp_data[8], border)
worksheet.write(row, col + 4, temp_data[4], border)
worksheet.write(row, col + 5, temp_data[5], border)
worksheet.write(row, col + 6, temp_data[6], border)
row += 1

## Clocse Workbook

I want make result below picture.

enter image description here

Answer Source

Question: I want same data cell merged using key data

Solution using openpyxl, for instance

Precondition: Ordered in key data

from openpyxl import Workbook
from openpyxl.styles import Alignment

wb = Workbook()
# Select First Worksheet
ws = wb.worksheets[0]

# Append Data from File
with open('last_emc.txt') as fh:
    for row_data in fh:

key_column = 5
merge_columns = [8, 9]
start_row = 2
max_row = ws.max_row
key = None

# Iterate all rows in `key_colum`
for row, row_cells in enumerate(ws.iter_rows(min_col=key_column, min_row=start_row,
                                             max_col=key_column, max_row=max_row),
    if key != row_cells[0].value or row == max_row:
        if row == max_row: row += 1
        if not key is None:
            for merge_column in merge_columns:
                ws.merge_cells( start_row=start_row, start_column=merge_column,
                                end_row=row - 1, end_column=merge_column)

                ws.cell(row=start_row, column=merge_column).\
                    alignment = Alignment(horizontal='center', vertical='center')

            start_row = row

        key = row_cells[0].value


enter image description here

Tested with Python: 3.4.2 - openpyxl: 2.4.1

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download