python 版 3.6.8 需要安装 阿里云插件 xlwings 模块 需要安装的 aliyun-python-sdk aliyun-python-sdk-core aliyun-python-sdk-ecs ######### 电脑上需要装有 office 软件,阿里云账号的 ACCESS key写成一个文本文件############## from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException ## 阿里云 ECS 请求,实例描述,,调用:DescribeInstances,查询一台或多台ECS实例的详细信息 from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest import json,time,os import xlwings as xw lst1 = __file__.split('/') base_path = '/'.join(lst1[:-1]) sk = '/'.join([base_path,'ask2']) ## 导出 多个 阿里云账号下面的 多个ECS,并保存为 xlsx文件,函数版。 ## 下载 ECS 实例,保存为 xlsx 文件 def download_ecs(): with open(sk) as f: ## 读取 存储ACCESSKey文件 while True: lst2 = [] line = f.readline() if len(line) == 0: break lst = line.strip().split('|') accessKeyId, accessSecret, region,account = lst[0],lst[1],lst[2],lst[3] client = AcsClient(accessKeyId, accessSecret, region) # AK,SK,Region request = DescribeInstancesRequest() # 查询ECS实例信息 request.set_PageSize(100) try: response = client.do_action_with_exception(request) reslut_s = response.decode('utf-8')##转成str dict1 = json.loads(reslut_s) ##转成 字典 lst1 = dict1["Instances"]["Instance"] if len(lst1) == 0: print(lst[3]+'账号下面,没有ECS实例。') continue for i in range(len(lst1)): # for k, v in lst1[i].items(): # print(k,v) #print(lst1)## 返回的是一个列表套字典,一个实例,是列表的一个元素,也就是一个,字典。 dict2 = lst1[i] lst2.append(dict2['InstanceName']) lst2.append(dict2['OSName']) lst2.append(dict2['Status']) lst2.append(dict2['Description']) lst2.append(dict2['PublicIpAddress']['IpAddress'][0])##公网IP ## 内网IP lst2.append(dict2['VpcAttributes']['PrivateIpAddress']['IpAddress'][0]) ##弹性IP lst2.append(dict2['EipAddress']['IpAddress']) lst2.append(dict2['Cpu']) lst2.append(dict2['Memory'] // 1024 ) lst2.append(dict2['CreationTime']) lst2.append(dict2['ExpiredTime']) #lst2.append(dict2['ConnectionString']) #print(len(lst2)) app = xw.App(visible=False, add_book=False) wb = app.books.add() lst = ['阿里云账号','名称', '操作系统', '状态', '描述','公网IP', '内网IP','弹性IP', 'CPU核数','内存(G)','创建时间','过期时间'] ## 标题行,从A1 单元格开始往后写 wb.sheets['sheet1'].range('A1').value = lst for i in range(0,len(lst2),11): rows = wb.sheets['sheet1'].used_range.last_cell.row ## ## 从第二行,第1列 开始写入,我们提取出来的数据内容 wb.sheets['sheet1'].range(rows + 1, 2).value = lst2[i:i+11] ## 每行的第一列,写入阿里云账号 wb.sheets['sheet1'].range(rows + 1, 1).value = account rows = rows + 1 ## 设置自动列宽, wb.sheets['sheet1'].range('A1:L{}'.format(rows)).columns.autofit() # print(rows) # 设置行高 wb.sheets['sheet1'].range('A1', 'L{}'.format(rows)).row_height = 20 # 设置 单元格 ,底纹 填充 用 rgb 数字代替, wb.sheets['sheet1'].range('A1:L1').color = (0, 0, 128) # 设置 单元格字体颜色 wb.sheets['sheet1'].range('A1:L1').api.Font.Color = 0xffffff ## 字体加粗 wb.sheets['sheet1'].range('A1:L1').api.Font.Bold = True ## 设置字体大小 wb.sheets['sheet1'].range('A1:L1').api.Font.Size = 12 # sheet1 表 更改名称 wb.sheets['sheet1'].name = 'ECS' ## 保存文件 new_dir = time.strftime("%Y-%m-%d", time.localtime()) if not os.path.exists('d:/02_ecs_mx/{}'.format(new_dir)): os.mkdir('d:/02_ecs_mx/{}'.format(new_dir)) new_file = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) wb.save('d:/02_ecs_mx/{}/{}_ecs-{}.xlsx'.format(new_dir,account,new_file)) wb.close() app.quit() print('{} 账号 ECS实例 下载完成,d:/02_ecs_mx/{}/{}_ecs-{}.xlsx'.format(account,new_dir,account,new_file)) except ServerException as e: print(e) except ClientException as e: print(e) download_ecs() ############################## 执行结果:
![]()