Python 遍历Sheet 每个Sheet都单独保存为一个Excel

2021-05-08 16:29

阅读:454

标签:xlsx   imp   pre   blog   openpyxl   title   https   内容   html   

原文:https://www.cnblogs.com/math98/p/10971342.html

#  把各个Sheet另存为单独的Excel
from openpyxl import load_workbook,Workbook


wb = load_workbook("excel1.xlsx")
sheetnames = wb.sheetnames

for name in sheetnames:
    ws = wb.get_sheet_by_name(name)
    print(ws)
    # 创建新的Excel
    wb2 = Workbook()
    # 获取当前sheet
    ws2 = wb2.active
    #两个for循环遍历整个excel的单元格内容
    for i,row in enumerate(ws.iter_rows()):
        for j,cell in enumerate(row):
            # 写入新Excel
            ws2.cell(row=i+1, column=j+1, value=cell.value)
            # 设置新Sheet的名称
            ws2.title = name

    wb2.save(name + ".xlsx")

Python 遍历Sheet 每个Sheet都单独保存为一个Excel

标签:xlsx   imp   pre   blog   openpyxl   title   https   内容   html   

原文地址:https://www.cnblogs.com/guxingy/p/13176613.html


评论


亲,登录后才可以留言!