编程语言
首页 > 编程语言> > python 操作 excel 表格

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')  #完成以后保存表格文件

 

标签:sheet,wb,表格,python,text,excel,print,import,openpyxl
来源: https://www.cnblogs.com/orientation/p/16471372.html