2017-04-05 22 views
-1

我想從我的excel sheet.Please刪除前20行建議如下,但沒有提到working.Installed所有openpyxl的solution.Tried許多方法來刪除Excel中的行,xlrd所以請幫助如何使用python

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") 

回答

0

當您考慮excel時,請使用熊貓。 (它可能是在後臺使用xlrd,但它是R/Spreadsheet data/CSV /等的一個保護傘。)刪除是對信息的不喜歡。想想通過選擇除了你不想要的行以外的所有東西來保存文件。

輸入參數可能會根據列標題的位置和其他內容而改變。如果你可以提供你的數據,你會得到更有針對性的答案。

使用documentation for read_excelto_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]) 
+0

1.Header是21行和運行此我失去頭之後,而不是它的標題顯示我\t無名:0 \t無名:1 \t無名:\t無名:3 \t無名:4 \t無名:5 \t未命名:6。第二個問題是 其增加了另一列與編號1,2,3 ....等,所以我不知道爲什麼發生這種情況。 – Gouri

+0

更改了代碼以將其包含在read_excel()fn中。 – Back2Basics