同步操作将从 mktime/python-learn 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import osimport xlrdimport xlwtfrom xlutils.copy import copyfrom xlutils.filter import process,XLRDReader,XLWTWriterimport datetimedef copy2(wb):w = XLWTWriter()process(XLRDReader(wb,'unknown.xls'),w)return w.output[0][1], w.style_list# 估值表类型# 中债V_CNBD = 0x0001# 中证V_CSI1 = 0x0002# 交易场所类型# 银行间EX_IB = 0x1001# 交易所EX_SHSZ = 0x1002def generate_excel(PATH):today = datetime.datetime.today().strftime("%Y%m%d")today2 = datetime.datetime.today().strftime("%Y-%m-%d")output = "债券估值导入模板.xls"outputdate = ""datefreq = {}files = [x for x in os.listdir(PATH) if x.endswith('.xls')and (x.find('中债估值2')>= 0 or x.find('中证估值2')>=0)]print(files)# 保存解析到的债券信息global_dict = {}for item in files:print("开始解析文件:{}".format(item))# 估值表类型: 中债估值(cnbd)、中证估值(csi1)file_type = None# 交易场所类型: 银行间(IB)、交易所(SHSZ)ex_type = Noneif item.find('中债') >= 0:file_type = V_CNBDelse:file_type = V_CSI1if item.find('.IB') > 0:ex_type = EX_IBelse:ex_type = EX_SHSZ# 债券IDcode = item[item.find('(')+1:item.find(')')].split('.')code.reverse()code = ''.join(code)if code not in global_dict:global_dict[code] = {}global_dict[code]['value_date'] = ''global_dict[code]['remain_date'] = ''global_dict[code]['dirty_price'] = ''global_dict[code]['net_value'] = ''global_dict[code]['modidura_value'] = ''global_dict[code]['cnvxty_value'] = ''global_dict[code]['vobp_value'] = ''global_dict[code]['yield_value'] = ''wb = xlrd.open_workbook(os.path.join(PATH, item))sheet = wb.sheets()[0]# bugfix: 2020年09月15日 17:00:51#data = sheet.row(1)data = [ x for x in sheet.get_rows() if x[1].value == '推荐' ][0]# 文件日期file_date = item[4:14].replace('-', '/')global_dict[code]['file_date'] = file_date# 估值日期value_date = data[0].value.split('-')# 统计估值日期出现频率key = "".join(value_date)if not key in datefreq:datefreq[key] = 1else:datefreq[key] += 1value_date.reverse()final_value_date = "/".join(value_date)#final_value_date = '/'.join([x for x in map(lambda x: str(int(x)), value_date)])global_dict[code]['value_date'] = final_value_date# 剩余期限 只有中债文件有remain_date = ""if file_type == V_CNBD:remain_date = data[2].valueglobal_dict[code]['remain_date'] = remain_date# 市价全价dirty_price = ""# 银行间使用中债的价格if file_type == V_CNBD and ex_type == EX_IB:dirty_price = data[3].valueelif file_type == V_CSI1 and ex_type == EX_SHSZ:dirty_price = data[5].valueif global_dict[code]['dirty_price'] == "":global_dict[code]['dirty_price'] = dirty_price# 市价净价net_value = ""if file_type == V_CNBD and ex_type == EX_IB:net_value = data[5].valueelif file_type == V_CSI1 and ex_type == EX_SHSZ:net_value = data[7].valueif global_dict[code]['net_value'] == "":global_dict[code]['net_value'] = net_value# 市价久期modidura_value = ""if file_type == V_CNBD and ex_type == EX_IB:modidura_value = data[8].valueelif file_type == V_CSI1 and ex_type == EX_SHSZ:modidura_value = data[9].valueif global_dict[code]['modidura_value'] == "":global_dict[code]['modidura_value'] = modidura_value# 市价凸性cnvxty_value = ""if file_type == V_CNBD and ex_type == EX_IB:cnvxty_value = data[9].valueelif file_type == V_CSI1 and ex_type == EX_SHSZ:cnvxty_value = data[10].valueif global_dict[code]['cnvxty_value'] == "":global_dict[code]['cnvxty_value'] = cnvxty_value# 基点价值vobp_value = ""if file_type == V_CNBD:vobp_value = data[10].valueglobal_dict[code]['vobp_value'] = vobp_value# 市价收益率yield_value = ""if file_type == V_CNBD and ex_type == EX_IB:yield_value = data[6].valueelif file_type == V_CSI1 and ex_type == EX_SHSZ:yield_value = data[8].valueif global_dict[code]['yield_value'] == "":global_dict[code]['yield_value'] = yield_value#print(global_dict)# 打开模板excel文件 并保留原文件格式rdbook = xlrd.open_workbook(os.path.join(PATH, output), formatting_info=True)# 获取总行数rdsheet = rdbook.sheet_by_index(0)# 获取拷贝wtbook, style_list = copy2(rdbook)# 获取xlwd可以操作的sheeetwtsheet = wtbook.get_sheet(0)rownum = 1for code in global_dict.keys():# 债券IDxf_index = rdsheet.cell_xf_index(0, 0)wtsheet.write(rownum, 0, code, style_list[xf_index])# 估值日xf_index = rdsheet.cell_xf_index(0, 1)wtsheet.write(rownum, 1, global_dict[code]['value_date'], style_list[xf_index])# 剩余期限xf_index = rdsheet.cell_xf_index(0, 2)wtsheet.write(rownum, 2, global_dict[code]['remain_date'], style_list[xf_index])# 市价全价xf_index = rdsheet.cell_xf_index(0, 3)wtsheet.write(rownum, 3, global_dict[code]['dirty_price'], style_list[xf_index])# 市价净价xf_index = rdsheet.cell_xf_index(0, 4)wtsheet.write(rownum, 4, global_dict[code]['net_value'], style_list[xf_index])# 市价久期xf_index = rdsheet.cell_xf_index(0, 5)wtsheet.write(rownum, 5, global_dict[code]['modidura_value'], style_list[xf_index])# 市价凸性xf_index = rdsheet.cell_xf_index(0, 6)wtsheet.write(rownum, 6, global_dict[code]['cnvxty_value'], style_list[xf_index])# 基点价值xf_index = rdsheet.cell_xf_index(0, 7)wtsheet.write(rownum, 7, global_dict[code]['vobp_value'], style_list[xf_index])# 市价收益率xf_index = rdsheet.cell_xf_index(0, 12)wtsheet.write(rownum, 12, global_dict[code]['yield_value'], style_list[xf_index])rownum += 1outputdate = sorted(datefreq.items(), key=lambda kv:(kv[1], kv[0]), reverse=True)[0][0]wtbook.save(os.path.join(PATH, '债券估值导入{}.xls'.format(outputdate)))print('处理完成')return "处理成功"if __name__ == '__main__':path = input()ret = generate_excel(path)print(ret)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。