使用 python 根据页数拆分 excel 文件
split excel file based on the number of sheets using python
我正在编写一个 python 脚本,它读取 excel 文件并根据页数将其拆分为多个文件。为此,我正在使用 xlwings package
问题是当我 运行 脚本崩溃并显示以下错误时:
with xw.App(visible = False) as app: AttributeError: enter
代码
from pathlib import Path
import xlwings as xw
base_dir = Path(__file__).parent
output_dir = base_dir / "output"
output_dir.mkdir(parents=True,exist_ok=True)
excel_file = "O:/WorkFiles/test.xlsm"
with xw.App(visible=False) as app:
wb = app.books.open(excel_file)
for sheet in wb.sheets:
wb_new = app.books.add()
sheet.copy(after=wb_new.sheets[0])
wb_new.sheets[0].delete()
wb_new.save(f"{sheet.name}.xlsx")
wb_new.close()
我假设这一行有错误:
with xw.App(visible=False) as app:
但没有它,您的代码也能正常工作。玩得开心
from pathlib import Path
import xlwings as xw
base_dir = Path(__file__).parent
output_dir = base_dir / "output"
output_dir.mkdir(parents=True,exist_ok=True)
excel_file = "O:/WorkFiles/test.xlsm"
app = xw.App(visible=False)
wb = xw.Book(excel_file)
for sheet in wb.sheets:
wb_new = app.books.add()
sheet.copy(after=wb_new.sheets[0])
wb_new.sheets[0].delete()
wb_new.save(f"{sheet.name}.xlsx")
wb_new.close()
我正在编写一个 python 脚本,它读取 excel 文件并根据页数将其拆分为多个文件。为此,我正在使用 xlwings package
问题是当我 运行 脚本崩溃并显示以下错误时:
with xw.App(visible = False) as app: AttributeError: enter
代码
from pathlib import Path
import xlwings as xw
base_dir = Path(__file__).parent
output_dir = base_dir / "output"
output_dir.mkdir(parents=True,exist_ok=True)
excel_file = "O:/WorkFiles/test.xlsm"
with xw.App(visible=False) as app:
wb = app.books.open(excel_file)
for sheet in wb.sheets:
wb_new = app.books.add()
sheet.copy(after=wb_new.sheets[0])
wb_new.sheets[0].delete()
wb_new.save(f"{sheet.name}.xlsx")
wb_new.close()
我假设这一行有错误:
with xw.App(visible=False) as app:
但没有它,您的代码也能正常工作。玩得开心
from pathlib import Path
import xlwings as xw
base_dir = Path(__file__).parent
output_dir = base_dir / "output"
output_dir.mkdir(parents=True,exist_ok=True)
excel_file = "O:/WorkFiles/test.xlsm"
app = xw.App(visible=False)
wb = xw.Book(excel_file)
for sheet in wb.sheets:
wb_new = app.books.add()
sheet.copy(after=wb_new.sheets[0])
wb_new.sheets[0].delete()
wb_new.save(f"{sheet.name}.xlsx")
wb_new.close()