Python xlrd splits the workbook sheet of the same excel table into multiple xlsx sheets named sheetname
編輯:Python
# -*- coding: utf-8 -*-
import xlrd
from openpyxl import Workbook
# Change to the name of the table you want to split
wb = xlrd.open_workbook(" Output one by one ( Shanghai and Shenzhen stocks ).xls")
sheetnames = wb.sheet_names()
print("sheetnames",sheetnames)
# Traverse sheet
for name in sheetnames:
ws = wb.sheet_by_name(name)
# Create a new excel surface
wb2 = Workbook()
# Get current sheet
ws2 = wb2.active
# Traverse excel Table contents and write new excel surface
for i, row in enumerate(ws.get_rows()):
for j, cell in enumerate(row):
# Write new Excel
ws2.cell(row=i + 1, column=j + 1, value=cell.value)
# Set new Sheet The name of
ws2.title = name
wb2.save(name + ".xlsx")