python 操作 excel 表格


openpyxl 用法实例

 

import requests
import openpyxl
import json
import time

res = requests.request('GET', 'http://1.2.32.2:23/data/getsites')
text = res.text
res2 = json.loads(text)
wb = openpyxl.load_workbook('zwy.xlsx')
sheet = wb['Sheet1']

a = 1
for i in res2['data']:
    print(i)
    if a == 1: #
        b = 0
        for k, v in i.items():
            print(k, v)
            sheet[a][b].value = k   #给表格赋值
            sheet[a + 1][b].value = v
            b += 1
    else:
        b = 0
        for k, v in i.items():
            print(k, v)
            sheet[a + 1][b].value = v
            b += 1
    a += 1
wb.save('zwy.xlsx')  #完成以后保存表格文件

 

原创文章,作者:506227337,如若转载,请注明出处:https://blog.ytso.com/tech/python/273868.html

(0)
上一篇 2022年7月12日 19:54
下一篇 2022年7月12日 20:32

相关推荐

发表回复

登录后才能评论