-1

I want to delete top 20 rows from my excel sheet.Please suggest the solution.Tried many methods as mentioned below but not working.Installed all openpyxl ,xlrd so please help

workbook = xlrd.open_workbook('myfile.xlsx')
worksheet = workbook.sheet_by_index(0)
def delete_row(self, row, shift=directionUp):
    """
Delete the entire 'row'.
    """
self.get_range('a%s' % row).EntireRow.Delete(Shift=shift)
# worksheet.getCells().deleteRows(2,10,True)
#  
# # Saving the modified Excel file in default (that is Excel 2003) format
# workbook.save(self.dataDir + "Delete Multiple Row    s.xls")
Mark Fitzgerald
  • 3,048
  • 3
  • 24
  • 29
Gouri
  • 1
  • 2
  • 7

1 Answers1

0

When you think about excel, use pandas. (it might be using xlrd in the background, but it's a umbrella for R/Spreadsheet data/CSV/etc..) Deleting is an active dislike of information. Think of saving the file by selecting everything but the rows you don't want.

The input paramaters could change depending on where your column headings are and other things. If you could put up your data you would get a more targeted answer.

You can get even more specific using the documentation for read_excel and to_excel.

import pandas as pd
data = pd.read_excel('myfile.xlsx', header=20, skiprows=19)
data[20:].to_excel('myfile.xlsx', columns=[insertCol1,insertCol2,insertCol3])
Back2Basics
  • 7,406
  • 2
  • 32
  • 45
  • 1.Header is in the 21st row and after running this I am loosing header ,instead of header its shows me Unnamed: 0 Unnamed: 1 Unnamed: Unnamed: 3 Unnamed: 4 Unnamed: 5 Unnamed: 6 . and second issue is its adding one another column with with numbering 1,2,3....etc so I am not sure why this is happening. – Gouri Apr 06 '17 at 05:21