Search code examples
pythonexcelpyexcelerator

pyexcelerator: how to delete a excel-sheet by sheet-name


As titled, I'm using pyexcelerator to operate excel files.

Code:

from pyExcelerator import parse_xls
fpath='D:\\Capacity_20120811.xls'
eData=parse_xls(fpath)




>>> eData
[(u'testExcelReport_hr',
  {(0, 0): u'Deletetime',
   (0, 1): u'Food',
   (0, 2): u'Peak Rate',
   (0, 3): u'Total Bytes',
   (0, 4): u'Total Msg No',
   (1, 0): u'20110824T05',
   (1, 1): u'111BSA',
   (1, 2): 6255326.0,
   (1, 3): 16226057.0,
   (1, 4): 127.0,
   (2, 0): u'20110824T06',
   (2, 1): u'111BSA',
   (2, 2): 352978.0,
   (2, 3): 672104.0,
   (2, 4): 2124.0})]

I want delete sheet by sheetname:'testExcelReport_hr'


Solution

  • You could use a list comprehension for that:

    eData = [(sheet_name, sheet_data) for (sheet_name, sheet_data) in eData
             if sheet_name != u'testExcelReport_hr']
    

    This keeps all sheets except for the one named testExcelReport_hr, in effect removing that sheet.