码迷,mamicode.com
首页 > 编程语言 > 详细

Python 3实现把各个Sheet另存为单独的Excel

时间:2019-01-16 18:10:27      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:col   dsd   设置   python   循环   写入   esc   pytho   save   

# -*- coding: utf-8 -*-
# @Time       : 2019/1/16 12:55
# @Author     : Philly
# @File       : sdsd.py
# @Description: 把各个Sheet另存为单独的Excel

from openpyxl import load_workbook,Workbook


wb = load_workbook("Sheet.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 3实现把各个Sheet另存为单独的Excel

标签:col   dsd   设置   python   循环   写入   esc   pytho   save   

原文地址:https://www.cnblogs.com/liuliu3/p/10278158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!