如何在不更改 .xls 文件的旧 sheet 的情况下 create/Delete sheet?

How to create/Delete sheet without changing old sheets for .xls files?

我尝试使用 xlsxwriter.add_worksheet() 创建新的 sheet 但它改变了我的旧 sheets.Any 库,它可以创建一个新的 sheet 而不会影响其他 sheets 和我如何删除 .xls 文件中的 sheet 因为我没有在 xlrd 或 xlsxwriter 中找到任何可以删除 sheet.

的函数

编辑:1 尝试使用 win32 模块。能够删除 sheet 但无法在不影响现有 sheet 的情况下创建新的 sheet。

更新:找到答案

创建一个新的 sheet :

import xlrd
xl_ref = xlrd.open_workbook('Path to file')

这里的type(xl_ref)是<class 'xlrd.book.Book> 所以当我们调用function

xl_ref.add_sheet('Test1')

基本上每当我们想在不影响现有 changes.first 的情况下操作 .xls 文件时,都会出现错误 book object has no attribute.So,转换为 workbook 对象。

from xlutils.copy import copy
xlwb_ref=copy(xl_ref)
xlwb_ref.add_sheet('Test1')
xlwb_ref.save('Path to file')

参考:Python_doc

删除Sheet:

    import pythoncom
    from win32com.client.gencache import EnsureDispatch
    pythoncom.CoInitialize() #if Calling same thing again and again so better reinitialize. 
    excel = EnsureDispatch("Excel.Application")
    excel_file = excel.Workbooks.Open("Path to file")
    sheet = excel.Sheets("Sheet name")
    sheet.Delete()
    excel_file.Close(True)