2017-08-09 124 views
0

親愛的,全部。如何刪除python python中的excel行

如果單元格(i,5)沒有值,我想在python 3.6中用python pywin32刪除excel中的行。

這是我的代碼。

def qms(self): 
    fname = QtWidgets.QFileDialog.getOpenFileName(self) 

    if fname[0]: 
     self.Filename.setText(fname[0]) 
     excel = win32com.client.Dispatch("Excel.Application") 
     excel.Visible = True 
     f = excel.Workbooks.Open(fname[0]) 
     fs = f.ActiveSheet 
     lastrow = fs.UsedRange.Rows.Count 

     for i in range(3, lastrow): 
      if not fs.Cells(i,5).Value : 
       fs.getCells().deleteRows(i-1,1,True) 

     excel.Workbooks.save() 
     excel.Quit() 

我認爲fs.getCells()。deleteRow(i-1,1,True)有問題。 我無法理解。你可以幫幫我嗎?

回答

0
def qms(self): 
    fname = QtWidgets.QFileDialog.getOpenFileName(self) 

    if fname[0]: 
     self.Filename.setText(fname[0]) 
     excel = win32com.client.Dispatch("Excel.Application") 
     excel.Visible = True 
     f = excel.Workbooks.Open(fname[0]) 
     fs = f.ActiveSheet 
     lastrow = fs.UsedRange.Rows.Count 

     for i in range(lastrow, 2, -1): 
      if fs.Cells(i,5).Value == None or fs.Cells(i,5).Value == "": 
       fs.Rows(i).EntireRow.Delete()