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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| import xlwt import pandas as pd
class Merge_cell(): ''' 目标:根据某列值对指定列值进行合并 '''
def __init__(self, excel_path, df, key_col, col2): self.excel_path = excel_path self.df = (df.drop(columns='index') if 'index' in df.columns else df) self.key_col = key_col self.col2 = col2 self.wb = xlwt.Workbook(excel_path) self.worksheet = self.wb.add_sheet('sheet1')
def _get_idx(self): groups = self.df.groupby(self.key_col) idxs = [[groups.get_group(i).index.min() + 1, groups.get_group(i).index.max() + 1] for i in groups.size().index] return idxs, len(idxs)
def _get_content(self, idx, key): """
:param idxs: 索引 [[1,2],[3,4]] :return: 暂时保存合并单元的值 """ import numpy as np
if not pd.isna(self.df.at[idx, key]): temp = self.df.at[idx, key] else: temp = None return temp
def merged(self): import numpy as np if not self.key_col: self.df.to_excel(self.excel_path, index=False) return idxs, length = self._get_idx() line_cn = self.df.index.size cols = list(self.df.columns.values) column_number = {col: idx for idx, col in enumerate(cols)} if self.key_col not in cols: print("key_cols is not completely include object's columns") return False if not all([v in cols for i, v in enumerate(self.col2)]): print("merge_cols is not completely include object's columns") return False for value, i in column_number.items(): self.worksheet.write(0, i, value) for key, idx in column_number.items(): if key not in self.col2: for i in range(line_cn): value = self.df.loc[i, key] if not pd.isna(value): self.worksheet.write(i + 1, idx, str(value)) else: pass else: for j in idxs: value = self._get_content(j[0] - 1, key) if value: self.worksheet.write_merge(j[0], j[1], idx, idx, value) else: pass self.wb.save(self.excel_path) if __name__ == '__main__': te = {'A': [1, 2, 2, 2, 3, 3], 'B': [1, 1, 1, 1, 1, 1], 'C': [1, 1, 1, 1, 1, 1], 'D': [1, 1, 1, 1, 1, 1]} t_f = pd.DataFrame(te) DF = Merge_cell('000_1.xls', t_f, 'A', ['B', 'C']) DF.merged()
|