python excel 行宽

import xlwt

# 创建excel表
workbook = xlwt.Workbook(encoding="utf-8")
# 设置excel表标题
sheet = workbook.add_sheet("生意参谋")
# 设置内容居中
style = xlwt.XFStyle()
center = xlwt.Alignment()
center.horz = 0x02
style.alignment = center
# 设置excel表头部信息,也就是第一行
head = [
    "排名", "搜索词", "搜索人气", "点击人气", "交易指数", "支付转化率", "在线商品数"
]
for i in range(len(head)):
    # 将设置好的头信息写入excel表第一行,第一个参数为行,第二个参数为列,第三个参数是要写入的头部信息,第四个是样式
    sheet.write(0, i, head[i], style)

# 设置行宽 col()中的参数为第几列
# 256表示是一个字符的长度,256 * 30 就是四十个字符的长度
for i in range(0, len(head)):
    exec('first_col_{} = sheet.col({})'.format(i + 1, i))
    exec('first_col_{}.width = 256 * 30'.format(i + 1))

# 将数据存入excel表
a = 1
for s in decrypt_data_list: # 此处的decrypt_data_list是爬虫获取到的数据列表,列表里的元素是字典类型
    # 行,列,内容,样式
    sheet.write(a, 0, s["排名"], style)
    sheet.write(a, 1, s["搜索词"], style)
    sheet.write(a, 2, s["搜索人气"], style)
    sheet.write(a, 3, s["点击人气"], style)
    sheet.write(a, 4, s["交易指数"], style)
    sheet.write(a, 5, s["支付转化率"], style)
    sheet.write(a, 6, s["在线商品数"], style)

    a += 1
# 写入excel的路径
workbook.save(new_csv)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等风来不如追风去

有钱的捧个钱场,没钱的捧个人场

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值