用python中类的方法写,用数据结构存储excel中下面列名的列: 客户需求 一级模块 二级模块 三级模块 功能用户 功能用户需求 触发事件 功能过程 子过程描述 数据移动类型 数据组 数据属性 CFP ,并通过合并单元格格式进行层级划分方便后续的jieba分词数据相似性分析,并写回excel表格中
时间: 2024-04-06 10:31:36 浏览: 85
好的,根据您的要求,可以使用Python中的openpyxl库来读取和写入Excel表格,使用类的方法进行封装。具体实现步骤如下:
1. 导入所需库文件
```python
import openpyxl
from openpyxl.styles import Alignment, PatternFill, Border, Side
```
2. 定义一个类,包含读写Excel表格的方法
```python
class ExcelHandler:
def __init__(self, file_path):
self.file_path = file_path
self.workbook = openpyxl.load_workbook(self.file_path)
self.sheet = self.workbook.active
def read_excel(self):
# 读取Excel表格中的数据
data = []
for row in self.sheet.iter_rows(min_row=2, max_col=13, values_only=True):
data.append(list(row))
return data
def write_excel(self, data):
# 写入Excel表格中的数据
for row_index, row_data in enumerate(data, start=2):
for col_index, value in enumerate(row_data, start=1):
self.sheet.cell(row=row_index, column=col_index, value=value)
self.workbook.save(self.file_path)
```
3. 定义一个方法,用于合并单元格并设置格式
```python
def merge_cells(sheet, start_row, end_row, start_col, end_col, text):
# 合并单元格
sheet.merge_cells(start_row=start_row, end_row=end_row, start_column=start_col, end_column=end_col)
# 设置文本
sheet.cell(row=start_row, column=start_col, value=text)
# 设置格式
fill = PatternFill(start_color="D9D9D9", end_color="D9D9D9", fill_type="solid")
border = Border(left=Side(style="thin"), right=Side(style="thin"), top=Side(style="thin"), bottom=Side(style="thin"))
alignment = Alignment(horizontal="center", vertical="center")
for row in range(start_row, end_row + 1):
for col in range(start_col, end_col + 1):
cell = sheet.cell(row=row, column=col)
cell.fill = fill
cell.border = border
cell.alignment = alignment
```
4. 定义一个方法,用于将数据存储到字典中,并进行层级划分
```python
def data_to_dict(data):
# 定义字典
result_dict = {}
# 遍历数据
for row_data in data:
# 获取每个单元格的值
customer_demand, first_module, second_module, third_module, function_user, function_user_demand, trigger_event, function_process, sub_process, data_move_type, data_group, data_attribute, cfp = row_data
# 根据层级划分存储到字典中
if customer_demand:
result_dict[customer_demand] = {}
if first_module:
result_dict[customer_demand][first_module] = {}
if second_module:
result_dict[customer_demand][first_module][second_module] = {}
if third_module:
result_dict[customer_demand][first_module][second_module][third_module] = {}
if function_user:
result_dict[customer_demand][first_module][second_module][third_module][function_user] = {}
if function_user_demand:
result_dict[customer_demand][first_module][second_module][third_module][function_user][function_user_demand] = {}
if trigger_event:
result_dict[customer_demand][first_module][second_module][third_module][function_user][function_user_demand][trigger_event] = {}
if function_process:
result_dict[customer_demand][first_module][second_module][third_module][function_user][function_user_demand][trigger_event][function_process] = {}
if sub_process:
result_dict[customer_demand][first_module][second_module][third_module][function_user][function_user_demand][trigger_event][function_process][sub_process] = {}
if data_move_type:
result_dict[customer_demand][first_module][second_module][third_module][function_user][function_user_demand][trigger_event][function_process][sub_process][data_move_type] = {}
if data_group:
result_dict[customer_demand][first_module][second_module][third_module][function_user][function_user_demand][trigger_event][function_process][sub_process][data_move_type][data_group] = {}
if data_attribute:
result_dict[customer_demand][first_module][second_module][third_module][function_user][function_user_demand][trigger_event][function_process][sub_process][data_move_type][data_group][data_attribute] = {}
if cfp:
result_dict[customer_demand][first_module][second_module][third_module][function_user][function_user_demand][trigger_event][function_process][sub_process][data_move_type][data_group][data_attribute] = cfp
return result_dict
```
5. 定义一个方法,用于将字典转换为列表,并写回Excel表格
```python
def dict_to_data(dict_data):
# 定义列表
result_data = []
# 遍历字典
for customer_demand, first_module_dict in dict_data.items():
for first_module, second_module_dict in first_module_dict.items():
for second_module, third_module_dict in second_module_dict.items():
for third_module, function_user_dict in third_module_dict.items():
for function_user, function_user_demand_dict in function_user_dict.items():
for function_user_demand, trigger_event_dict in function_user_demand_dict.items():
for trigger_event, function_process_dict in trigger_event_dict.items():
for function_process, sub_process_dict in function_process_dict.items():
for sub_process, data_move_type_dict in sub_process_dict.items():
for data_move_type, data_group_dict in data_move_type_dict.items():
for data_group, data_attribute_dict in data_group_dict.items():
for data_attribute, cfp in data_attribute_dict.items():
# 将每行数据添加到列表中
result_data.append([customer_demand, first_module, second_module, third_module, function_user, function_user_demand, trigger_event, function_process, sub_process, data_move_type, data_group, data_attribute, cfp])
# 获取单元格合并的起始和结束位置
start_row = len(result_data) - 1
end_row = start_row + len(cfp) - 1
start_col = 13
end_col = 13
# 合并单元格并设置格式
merge_cells(sheet, start_row + 2, end_row + 2, start_col, end_col, cfp)
# 将处理后的数据写回Excel表格
excel.write_excel(result_data)
```
6. 定义一个方法,用于进行jieba分词数据相似性分析
```python
def jieba_similarity_analysis(dict_data):
pass # TODO: 进行jieba分词数据相似性分析
```
7. 主函数中调用以上方法
```python
if __name__ == '__main__':
# 定义Excel文件路径
file_path = 'example.xlsx'
# 创建ExcelHandler对象
excel = ExcelHandler(file_path)
# 读取Excel表格中的数据
data = excel.read_excel()
# 将数据存储到字典中,并进行层级划分
dict_data = data_to_dict(data)
# 将字典转换为列表,并写回Excel表格
dict_to_data(dict_data)
# 进行jieba分词数据相似性分析
jieba_similarity_analysis(dict_data)
```
以上就是根据您的要求,使用Python中的类的方法编写的读写Excel表格的程序。由于jieba分词数据相似性分析部分没有具体要求,因此暂时只是留下了一个TODO,需要您自己实现。
阅读全文