如何删除Excel中的行?
wb = xw.Book('Shipment.xlsx')
wb.sheets['Page1_1'].range('1:1').clear()
.clear()用于删除内容。我想删除该行。我很惊讶.clear()函数有效,但不是.delete()
任何建议都有帮助!谢谢
答案 0 :(得分:3)
我在Windows7上使用xlwings 0.11.7和Python 3.6.0。 我这样做,它可以很好地工作:
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()
答案 1 :(得分:1)
尝试使用 .Rows ,例如:
wb.sheets("Page1_1").Rows(1).Delete
您同样可以使用 .Columns 删除列:
wb.sheets("Page1_1").Columns(1).Delete
答案 2 :(得分:0)
for i in range(end+1, max_col):
workbook.sheets[sheet_name].api.Columns(end+1).Delete()
我这样做是为了删除一定范围的列,也应该与行一起使用,您只需要记住Columns / Rows会发生变化,所以我只停留在同一列上