1、转换python字典类型为dataframe
# !/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = # pandas >= 1.5.3 import pandas as pd pydict = {'Dosage': '1.1.1.1,2.2.2.2,4.4.4.4,5.5.5.5', 'HalfLife': '6.6.6.6,7.7.7.7,8.8.8.8', 'Cmax': '20.20.20.20'} df = pd.DataFrame(pd.Series(pydict), columns=['IP']) df = df.reset_index().rename(columns={'index': '组名'}) df.index = [i for i in range(1, len(df.index) + 1)] print(df)
2、excel表设置样式
#!/usr/bin/env python # -*- coding:utf-8 -*- #__author__ = # pandas >= 1.5.3 import pandas as pd data = [ {'switchname': 'RuiJie#S6510-48VS8CQ-01', 'mac': 'ac:1f:6b:f8:70:f3', 'interface': 'TFGigabitEthernet 0/20'}, {'switchname': 'RuiJie#S6510-48VS8CQ-01', 'mac': 'ac:1f:6b:f8:ba:c3', 'interface': 'TFGigabitEthernet 0/21'}, {'switchname': 'RuiJie#S6510-48VS8CQ-01', 'mac': 'ac:1f:6b:f8:bd:65', 'interface': 'TFGigabitEthernet 0/15'}, {'switchname': 'RuiJie#S6510-48VS8CQ-01', 'mac': 'ac:1f:6b:f8:c2:a1', 'interface': 'TFGigabitEthernet 0/16'}, {'switchname': 'RuiJie#S6510-48VS8CQ-02', 'mac': 'ac:1f:6b:f8:c6:ef', 'interface': 'TFGigabitEthernet 0/19'}, {'switchname': 'RuiJie#S6510-48VS8CQ-02', 'mac': 'ac:1f:6b:f8:ce:5f', 'interface': 'TFGigabitEthernet 0/18'}, {'switchname': 'RuiJie#S6510-48VS8CQ-02', 'mac': 'ac:1f:6b:f8:ce:61', 'interface': 'TFGigabitEthernet 0/17'}, {'switchname': 'RuiJie#S6510-48VS8CQ-02', 'mac': 'ac:1f:6b:f8:cf:dd', 'interface': 'TFGigabitEthernet 0/14'}, {'switchname': 'RuiJie#S6510-48VS8CQ-03', 'mac': 'b0:26:28:7a:94:30', 'interface': 'TFGigabitEthernet 0/6'}, {'switchname': 'RuiJie#S6510-48VS8CQ-03', 'mac': 'b0:26:28:7a:94:e0', 'interface': 'TFGigabitEthernet 0/5'}, {'switchname': 'RuiJie#S6510-48VS8CQ-03', 'mac': 'ac:1f:6b:f8:70:f2', 'interface': 'TFGigabitEthernet 0/41'}, {'switchname': 'RuiJie#S6510-48VS8CQ-03', 'mac': 'ac:1f:6b:f8:ba:c2', 'interface': 'TFGigabitEthernet 0/42'}, {'switchname': 'RuiJie#S6510-48VS8CQ-04', 'mac': 'ac:1f:6b:f8:bd:64', 'interface': 'TFGigabitEthernet 0/36'}, {'switchname': 'RuiJie#S6510-48VS8CQ-04', 'mac': 'ac:1f:6b:f8:c2:a0', 'interface': 'TFGigabitEthernet 0/37'}, {'switchname': 'RuiJie#S6510-48VS8CQ-04', 'mac': 'ac:1f:6b:f8:c6:ee', 'interface': 'TFGigabitEthernet 0/40'}, {'switchname': 'RuiJie#S6510-48VS8CQ-04', 'mac': '4c:00:0a:b6:22:0e', 'interface': 'TFGigabitEthernet 0/19'}] df = pd.DataFrame(data, columns=['switchname', 'interface', 'mac']) df = df.rename(columns={'switchname': '交换机名称', 'mac': 'MAC地址', 'interface': '接口名称'}) df.index = [i for i in range(1, len(df.index) + 1)] df = df.style.set_properties(**{ 'border': '1px solid black', # 所有边框 # 'border-left': '1px solid black', # 左边框 # 'border-right': '1px solid black', # 右边框 # 'border-top': '1px solid black', # 上边框 # 'border-bottom': '1px solid black', # 下边框 }) df.to_excel('abcd.xlsx')
参考链接:
https://blog.csdn.net/weixin_47661174/article/details/124697842 # Pandas DataFrame的合并