NinjaWarrior
NinjaWarrior

Reputation: 25

xlwings: Delete a col | row from Excel

How do I delete a row in Excel?

wb = xw.Book('Shipment.xlsx')
wb.sheets['Page1_1'].range('1:1').clear()

.clear() works to remove the content. I want to delete the row. I'm surprise the .clear() function works, but not .delete()

Any advice helps! Thank you

Upvotes: 4

Views: 15217

Answers (4)

Andrey
Andrey

Reputation: 6377

It is now possible to delete without using api property:

wb.sheets['Page1_1'].range('1:1').delete()

Upvotes: 7

Jakob Mitterauer
Jakob Mitterauer

Reputation: 1

for i in range(end+1, max_col):

        workbook.sheets[sheet_name].api.Columns(end+1).Delete()

I have done this to Delete a certain range of columns, should work with rows too, you just need to keep in mind that the Columns/Rows shift so i just stay at the same column

Upvotes: 0

Jack Cao
Jack Cao

Reputation: 91

I use xlwings 0.11.7 with Python 3.6.0 on my Windows7. I do this, and it can work very well :

import xlwings as xw
from xlwings.constants import DeleteShiftDirection

app = xw.App()
wb = app.books.open('name.xlsx')
sht = wb.sheets['Sheet1']

# Delete row 2
sht.range('2:2').api.Delete(DeleteShiftDirection.xlShiftUp) 

# Delete row 2, 3 and 4 
sht.range('2:4').api.Delete(DeleteShiftDirection.xlShiftUp) 

# Delete Column A
sht.range('A:A').api.Delete(DeleteShiftDirection.xlShiftToLeft)

# Delete Column A, B and C
sht.range('A:C').api.Delete(DeleteShiftDirection.xlShiftToLeft)

wb.save()
app.kill()

Upvotes: 6

Prebsus
Prebsus

Reputation: 695

Try using the .Rows, for example:

wb.sheets("Page1_1").Rows(1).Delete

And you would similarly use the .Columns for deleting columns:

wb.sheets("Page1_1").Columns(1).Delete

Upvotes: 2

Related Questions