我尝试使用xlsxwriter.add_worksheet()创建新工作表,但是它改变了我以前的sheets.Any库,它可以在不影响其他工作表的情况下创建一个新工作表,以及如何删除.xls文件中的工作表,因为我没有在xlrd或xlsxwriter中找到任何可以删除工作表的函数。
编辑:1尝试使用win32模块。能够删除工作表,但无法在不影响现有工作表的情况下创建新工作表。
发布于 2017-08-09 17:04:14
更新:找到答案
创建一个新的工作表:
import xlrd
xl_ref = xlrd.open_workbook('Path to file')这里的类型(Xl_ref)是<class 'xlrd.book.Book>,所以当我们调用函数时
xl_ref.add_sheet('Test1')当我们想要在不影响现有attribute.So的情况下操作.xls文件,转换成工作簿对象时,就会得到错误图书对象没有基本的changes.first。
from xlutils.copy import copy
xlwb_ref=copy(xl_ref)
xlwb_ref.add_sheet('Test1')
xlwb_ref.save('Path to file')参考资料:文档
删除工作表:
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)https://stackoverflow.com/questions/45590084
复制相似问题