Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete columns in xlwings?

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.

like image 444
MattiH Avatar asked Sep 24 '22 02:09

MattiH


People also ask

How do you delete a column in Excel using Python?

Python Delete Excel Rows and Columns In openpyxl, we can use delete_rows() and delete_cols() methods from the Worksheet object to remove entire rows and columns. The syntax is straightforward: delete_rows(row_id, number_of_rows) delete_cols(col_id, number_of_cols)

How do I use Python Xlwings?

Xlwings Excel Add-In But thanks to Xlwings we can now add a VBA macro into Excel, so we can start the Jupyter Notebook out of Excel. The basic concept is simply to write your Jupyter Notebook code into a main function, export that as a Python file (. py) and call that Python script out of Excel's VBA Editor.

Does Xlwings work on Linux?

Since v0. 26.0, xlwings can be installed on Linux servers in connection with Google Sheets or Excel on the web, see Remote Interpreter. xlwings requires at least Python 3.7.

What can Xlwings do?

Xlwings can be used to insert data in an Excel file similarly that it reads from an Excel file. Data can be provided as a list or a single input to a certain cell or a selection of cells.


1 Answers

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

xw.Range('C:C').xl_range.Delete()
like image 180
Aaron Christiansen Avatar answered Oct 17 '22 02:10

Aaron Christiansen