python处理excel

编辑excel时格式问题

在打开excel时,加入该参数formatting_info=True

 目前只兼容了xls

https://www.cnblogs.com/Detector/p/8709362.html

-------------------------------------------------------------------------------------------------------分割线-----------------------------------------------------------------------------------------------------------------

写入指定列数据:

先定义一个写入数据列的方法

def write_list_to_excel(file_name,row,column,lists):#lists 数据结构,形如:[[a,b],[c,d],...]
#往第几行开始第几列写数据
import xlwt
with open(file_name, "r", encoding="utf8", errors="ignore") as f:
# hostlist = []#其中一列
# iplist = []#另一列
hosts_ip = []
# 构造数据结构,形如:[[a,b],[c,d],...]
multi_list = map(list, zip(*lists))
# print(multi_list)
for multi in multi_list:
hosts_ip.append(multi)
# print(hosts_ip)
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('test', cell_overwrite_ok=True)
# 关键代码
for hi in enumerate(hosts_ip):
# 行、列、值
for i in range(len(hi[1])):
worksheet.write(hi[0] + row, column+i, hi[1][i])#往第hi[0]+row行第column列写入
workbook.save(file_name)
再获取数据源处理成list格式后调用前面定义的方法

def test_get_deposit(self):
datas=D.get_deposit_list(base_url=base_url,header=headers,pageSize=203,pageNo=1,bank_cde=[],applyStatus=['CS2_7'])#applyStatus=['CS2_7']
applyNumber=[]
accountId=[]
depositAmount=[]
for i in range(len(datas)):
applyNumber.append(datas[i]['applyNumber'])
accountId.append(datas[i]['accountId'])
depositAmount.append(datas[i]['depositAmount'])
# print(applyNumber,'/n',accountId,'/n',depositAmount)
print(applyNumber)
print(accountId)
print(depositAmount)
#将获取的数据写入excel
time.sleep(10)
write_list_to_excel(file_name=basedir+"\\data\\test\\test.xls", row=2, column=3,lists=[applyNumber, accountId, depositAmount])

 

 

效果类似于

if __name__=="__main__":
# data=read_line_tolist(file_name='F:\\CDMS_Interface\\data\\test\\Auto-account-open-uat2.xlsx',sheet_name='重測',usecols=[0])#usecols
# print(data)
hostlist=['D00000463', 'D00000462', 'D00000461', 'D00000460', 'D00000459', 'D00000458', 'D00000457', 'D00000456', 'D00000455', 'D00000454', 'D00000453', 'D00000452', 'D00000451', 'D00000450', 'D00000449', 'D00000448', 'D00000447', 'D00000446', 'D00000445', 'D00000444', 'D00000443', 'D00000442', 'D00000441', 'D00000440', 'D00000439', 'D00000438', 'D00000437', 'D00000436', 'D00000435', 'D00000434', 'D00000433', 'D00000432', 'D00000431', 'D00000430', 'D00000429', 'D00000428', 'D00000427', 'D00000426', 'D00000425', 'D00000424', 'D00000423', 'D00000422', 'D00000421', 'D00000420', 'D00000419', 'D00000418', 'D00000417', 'D00000416', 'D00000415', 'D00000414', 'D00000413', 'D00000412', 'D00000411', 'D00000410', 'D00000409', 'D00000408', 'D00000407', 'D00000406', 'D00000405', 'D00000404', 'D00000403', 'D00000402', 'D00000401', 'D00000400', 'D00000399', 'D00000398', 'D00000397', 'D00000396', 'D00000395', 'D00000394', 'D00000393', 'D00000392', 'D00000391', 'D00000390', 'D00000389', 'D00000388', 'D00000387', 'D00000386', 'D00000385', 'D00000384', 'D00000383', 'D00000382', 'D00000381', 'D00000380', 'D00000379', 'D00000378', 'D00000377', 'D00000376', 'D00000375', 'D00000374', 'D00000373', 'D00000372', 'D00000371', 'D00000370', 'D00000369', 'D00000368', 'D00000367', 'D00000366', 'D00000365', 'D00000364', 'D00000363', 'D00000362', 'D00000361', 'D00000360', 'D00000359', 'D00000358', 'D00000357', 'D00000356', 'D00000355', 'D00000354', 'D00000353', 'D00000352', 'D00000351', 'D00000350', 'D00000349', 'D00000348', 'D00000347', 'D00000346', 'D00000345', 'D00000344', 'D00000343', 'D00000342', 'D00000341', 'D00000340', 'D00000339', 'D00000338', 'D00000337', 'D00000336', 'D00000335', 'D00000334', 'D00000333', 'D00000332', 'D00000331', 'D00000330', 'D00000329', 'D00000328', 'D00000327', 'D00000326', 'D00000325', 'D00000324', 'D00000323', 'D00000322', 'D00000321', 'D00000320', 'D00000319', 'D00000318', 'D00000317', 'D00000316', 'D00000315', 'D00000314', 'D00000313', 'D00000312', 'D00000311', 'D00000310', 'D00000309', 'D00000308', 'D00000307', 'D00000306', 'D00000305', 'D00000304', 'D00000303', 'D00000302', 'D00000301', 'D00000300', 'D00000299', 'D00000298', 'D00000297', 'D00000296', 'D00000295', 'D00000294', 'D00000293', 'D00000292', 'D00000291', 'D00000290', 'D00000289', 'D00000288', 'D00000287', 'D00000286', 'D00000285', 'D00000284', 'D00000283', 'D00000282', 'D00000281', 'D00000280', 'D00000279', 'D00000278', 'D00000277', 'D00000276', 'D00000275', 'D00000274', 'D00000273', 'D00000272', 'D00000271', 'D00000270', 'D00000269', 'D00000268', 'D00000267', 'D00000266', 'D00000265', 'D00000264', 'D00000263', 'D00000262', 'D00000261']

iplist=['5001311210', '5001371210', '5001381210', '5001471210', '5001481210', '5001491210', '5001511210', '5001621210', '5001631210', '5001691210', '5001701210', '5001821210', '5001941210', '5001961210', '5001981210', '5001991210', '5002001210', '5002011210', '5002021210', '5002041210', '5002141210', '5002151210', '5002161210', '5002191210', '5002211210', '5002221210', '5002241210', '5002271210', '5002591210', '5002601210', '5002651210', '5002691210', '5002701210', '5002941210', '5003041210', '5003051210', '5003061210', '5003071210', '5003081210', '5003101210', '5003231210', '5003241210', '5003261210', '5003331210', '5003341210', '5003371210', '5003381210', '5003401210', '5003411210', '5003421210', '5003441210', '5003471210', '5003481210', '5003491210', '5003661210', '5003681210', '5003691210', '5003701210', '5003711210', '5003721210', '5003801210', '5003811210', '5003861210', '5003871210', '5003881210', '5003891210', '5003901210', '5003911210', '5003921210', '5003931210', '5003941210', '5003951210', '5003961210', '5003971210', '5003981210', '5003991210', '5004001210', '5004011210', '5004021210', '5004031210', '5004041210', '5004051210', '5004061210', '5004071210', '5004081210', '5004091210', '5004101210', '5004111210', '5004121210', '5004131210', '5004141210', '5004151210', '5004161210', '5004171210', '5004181210', '5004191210', '5004201210', '5004211210', '5004221210', '5004231210', '5004241210', '5004251210', '5004261210', '5004271210', '5004281210', '5004291210', '5004301210', '5004311210', '5004321210', '5004331210', '5004341210', '5004351210', '5004361210', '5004371210', '5004381210', '5004391210', '5004401210', '5004411210', '5004421210', '5004431210', '5004441210', '5004451210', '5004461210', '5004471210', '5004481210', '5004491210', '5004501210', '5004511210', '5004521210', '5004531210', '5004541210', '5004551210', '5004561210', '5004571210', '5004581210', '5004591210', '5004601210', '5004611210', '5004621210', '5004631210', '5004641210', '5004651210', '5004661210', '5004671210', '5004681210', '5004691210', '5004701210', '5004711210', '5004721210', '5004731210', '5004741210', '5004751210', '5004761210', '5004771210', '5004781210', '5004791210', '5004801210', '5004811210', '5004821210', '5004831210', '5004841210', '5004851210', '5004861210', '5004871210', '5004881210', '5004891210', '5004901210', '5004911210', '5004921210', '5004931210', '5004941210', '5004951210', '5004961210', '5004971210', '5004981210', '5004991210', '5005001210', '5005011210', '5005021210', '5005031210', '5005041210', '5005051210', '5005061210', '5005071210', '5005081210', '5005091210', '5005101210', '5005111210', '5005121210', '5005131210', '5005141210', '5005151210', '5005161210', '5005171210', '5005181210', '5005191210', '5005201210', '5005211210', '5005221210', '5005231210', '5005241210', '5005251210', '5005261210']

iplist2=['99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999', '99,999,999']

write_list_to_excel(file_name="D:\\My\\CDMS_Interface\\data\\test\\test.xlsx", row=2, column=3, lists=[hostlist,iplist,iplist2])

 

posted @ 2021-06-26 18:10  行走的小z  阅读(117)  评论(0编辑  收藏  举报