找回密码
 立即注册
搜索
热搜: Excel discuz
查看: 1311|回复: 0

[《从excel到python》笔记案例] 批量新建工作簿、工作表---while、for

[复制链接]

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750848
发表于 2022-1-23 17:50:49 | 显示全部楼层 |阅读模式
首先用while循环
  1. import xlwt
  2. year = 2020
  3. while year <2023:
  4.     nwb = xlwt.Workbook('utf-8')
  5.     mouth = 1
  6.     while mouth<13:
  7.         nws = nwb.add_sheet(f'{mouth}月')
  8.         mouth +=1
  9.     nwb.save(f'{year}年.xls')
  10.     year +=1
复制代码
用for循环代码更简洁易懂
  1. import xlwt
  2. for i in range(2010,2015):
  3.     nwb = xlwt.Workbook('utf-8')
  4.     for j in range(1,13):
  5.         nws = nwb.add_sheet(f'{j}月')
  6.     nwb.save(f'{i}年.xls')
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|五花八门论坛 ( 豫ICP备15031300号-3 )

GMT+8, 2024-4-24 22:01 , Processed in 0.087123 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表