Reputation: 397
Dear, All.
If cells (i,5) does not have value, I want to delete the row in excel with python pywin32 at python 3.6.
this is my code.
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()
I think there are problem in fs.getCells().deleteRow(i-1,1,True). I cannot understand. could you help me?
Upvotes: 0
Views: 6898
Reputation: 397
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()
Upvotes: 0
Reputation: 22876
for i in range(lastrow, 2, -1):
if fs.Cells(i,5).Value != ""
fs.Rows(i).EntireRow.Delete()
or you can probably delete all at once:
fs.UsedRange.Offset(2).Columns(5).SpecialCells(4).EntireRow.Delete()
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-specialcells-method-excel
Upvotes: 1