xlwt将文件写入execl文件,并着色

https://blog.csdn.net/kk123a/article/details/49813559
https://www.jianshu.com/p/0dd8cc68a3c3
在这里插入图片描述

from xlwt import *
import xlwt
import urllib.request
import json
import time

#创建execl样式----------------------------
style_yellow = XFStyle()
pattern = Pattern()
pattern.pattern = Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 5 #设置单元格背景色为黄色
style_yellow.pattern = pattern

style_green = XFStyle()
pattern = Pattern()
pattern.pattern = Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 3 #设置单元格背景色为绿色
style_green.pattern = pattern

style_blue = XFStyle()
pattern = Pattern()
pattern.pattern = Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 44 #设置单元格背景色为蓝色
style_blue.pattern = pattern

style_cyan = XFStyle()
pattern = Pattern()
pattern.pattern = Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 35 #设置单元格背景色为青色
style_cyan.pattern = pattern

#pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...
#-----------------------------------------


def json2execl(subject):#将接口查询结果写入execl文件
    title = list(subject[0])
    sheet.write(len(subject)+2,0,url)#在文件尾部写入调用的接口参数
    sheet.write(len(subject)+3,0,date)#在文件尾部写入调用接口的时间
    for i in range(len(title)):
        if title[i] in ['percentage']:
            sheet.write(0,i,title[i],style=style_yellow)#在文件头部写标题
        elif title[i] in ['poster','opmark','still']:
            sheet.write(0,i,title[i],style=style_green)#在文件头部写标题
        elif title[i] in ['column','columnname']:
            sheet.write(0,i,title[i],style=style_blue)#
        elif title[i] in ['seriestype']:
            sheet.write(0,i,title[i],style=style_cyan)#
        else:
            sheet.write(0,i,title[i])
    for i in range(len(subject)):#逐行读取
        line = list(subject[i].values())#获取list的值
        for j in range(len(line)):#逐列读取
            sheet.write(i+1,j,line[j])#逐行逐列写入文件

date=time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime())
command= 'gettopn'
type = 's'
#type = 'v'
#type = 't'
#type = 'c'
count = 20
excel = xlwt.Workbook(encoding='utf-8')
sheet = excel.add_sheet('xlwt_sheet1')
url = "http://xxx.xxx.xxx.240:8082/idp/"+command+"?type="+type+"&count="+str(count)
response=urllib.request.urlopen(url)
req=response.read()xxx_s = str(req, encoding='utf-8')
req_j=json.loads(req_s)
subject = req_j['subject']

json2execl(subject)

file = command+"_"+type+"_"+str(count)+"_"+date+".xls"
excel.save(file)#保存文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值