Python xlrd将同一个excel表的工作簿sheet拆分成多个以sheetname命名的xlsx表

# -*- coding: utf-8 -*-
 
import xlrd
from openpyxl import Workbook

# 换成自己要拆分表格的名字
wb = xlrd.open_workbook("逐股输出(沪深股).xls")
sheetnames = wb.sheet_names() 
print("sheetnames",sheetnames)

# 遍历sheet
for name in sheetnames:
    ws = wb.sheet_by_name(name)
    # 创建新的excel表
    wb2 = Workbook()
    # 获取当前sheet
    ws2 = wb2.active
    # 遍历excel表格内容并写入新的excel表
    for i, row in enumerate(ws.get_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,python)