python3下载多个RDS实例信息保存为xlsx文件 python 版 3.6.8 需要安装 阿里云插件 xlwings 模块 需要安装的 aliyun-python-sdk aliyun-python-sdk-core aliyun-python-sdk-rds ######### 电脑上需要装有 office 软件,阿里云账号的 ACCESS key写成一个文件##################### from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkrds.request.v20140815.DescribeDBInstancesRequest import DescribeDBInstancesRequest from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest import json,time,os import xlwings as xw lst1 = __file__.split('/') base_path = '/'.join(lst1[:-1]) sk = '/'.join([base_path,'ask1']) ## 下载 RDS 实例,保存为 xlsx 文件 def download_rds(): 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 = DescribeDBInstancesRequest() # 查询rds实例信息 request.set_PageSize(10) try: response = client.do_action_with_exception(request) reslut_s = response.decode('utf-8')##转成str dict1 = json.loads(reslut_s) ##转成 字典 lst1 = dict1["Items"]["DBInstance"] if len(lst1) == 0: print(lst[3]+'账号下面,没有RDS实例。') 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['DBInstanceId']) lst2.append(dict2['DBInstanceDescription']) lst2.append(dict2['Engine']) lst2.append(dict2['EngineVersion']) lst2.append(dict2['CreateTime']) lst2.append(dict2['ExpireTime']) lst2.append(dict2['ConnectionString']) #print(len(lst2)) app = xw.App(visible=True, add_book=False) wb = app.books.add() lst = ['阿里云账号','DBInstanceId(实例ID)', 'DBInstanceDescription(实例描述)', 'Engine(数据库类型)', 'EngineVersion(数据库版本)','CreationTime(创建时间)', 'ExpireTime(到期时间)', 'PrivateString(内网连接地址)'] ## 标题行,从A1 单元格开始往后写 wb.sheets['sheet1'].range('A1').value = lst for i in range(0,len(lst2),7): #print(lst2[i:i+7]) ## 从第二行,第1列 开始写入,我们提取出来的数据内容 rows = wb.sheets['sheet1'].used_range.last_cell.row wb.sheets['sheet1'].range(rows + 1, 1).value = lst2[i:i+7] wb.sheets['sheet1'].range(rows + 1, 8).value = account rows = rows + 1 ## 设置自动列宽, wb.sheets['sheet1'].range('A1:H{}'.format(rows)).columns.autofit() # print(rows) # 设置行高 wb.sheets['sheet1'].range('A1', 'A{}'.format(rows)).row_height = 20 # 设置 单元格 ,底纹 填充 用 rgb 数字代替, wb.sheets['sheet1'].range('A1:H1').color = (0, 0, 128) # 设置 单元格字体颜色 wb.sheets['sheet1'].range('A1:H1').api.Font.Color = 0xffffff ## 字体加粗 wb.sheets['sheet1'].range('A1:H1').api.Font.Bold = True ## 设置字体大小 wb.sheets['sheet1'].range('A1:H1').api.Font.Size = 12 # sheet1 表 更改名称 wb.sheets['sheet1'].name = 'RDS' ## 保存文件 new_dir = time.strftime("%Y-%m-%d", time.localtime()) if not os.path.exists('d:/02_rds_mx/{}'.format(new_dir)): os.mkdir('d:/02_rds_mx/{}'.format(new_dir)) new_file = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) wb.save('d:/02_rds_mx/{}/{}_rds-{}.xlsx'.format(new_dir,account,new_file)) wb.close() app.quit() print('{} 账号 RDS实例 下载完成,d:/02_rds_mx/{}/{}_rds-{}.xlsx'.format(account,new_dir,account,new_file)) except ServerException as e: print(e) except ClientException as e: print(e) download_rds() ########################### 执行结果:
![]()