How to delete columns in xlwings?

落爺英雄遲暮 提交于 2019-12-14 03:46:36

问题


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()


来源:https://stackoverflow.com/questions/35993930/how-to-delete-columns-in-xlwings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!