How to delete columns in xlwings?

2019-06-27 04:14发布

问题:

I'm using xlwings on Windows (Excel 2007 with Python 2.7) and would like to delete either ranges or columns with xlwings. As far as I could see, deletion of a range or a column is a missing feature, so I tried to follow the instructions given here and tried to access the .Delete method of Range object in VBA. Do you have any suggestions on what is causing the error and how to delete a range of whole column in xlwings?

The code I was trying to run in command line is below (for deleting the whole column in the active workbook):

import xlwings as xw
wb = xw.Workbook.active()
xw.Range('C1:C3').xl_range.EntireColumn.Delete

I received the following error:

bound method CDispatch.Delete of <COMObject <unknown>>>

Xlwings would offer the possibility to clear values from range (by Range('C1:C3').clear()), but that would leave an empty range or column to the sheet.

回答1:

Access the entire column and then use .xl_range.Delete() instead:

xw.Range('C:C').xl_range.Delete()