Prerna Gupta
Prerna Gupta

Reputation: 11

How to insert a new column in an excel workbook using xlwings library in Python?

I need to add a new empty column in between two columns of an existing excel using xlwings. How do I do that?

I need to use xlwings library itself as the project requirements need that library.

Please help me with the code

I am using this code :

import xlwings as xw
from xlwings.constants import DeleteShiftDirection
wb = xw.Book('input_file.xlsm')
wb.sheets['Sheet 1'].delete()
wb.sheets['Sheet 3'].delete()
sheet = wb.sheets['Sheet 2']
sheet.range('1:1').api.Delete(DeleteShiftDirection.xlShiftUp)
sheet.pictures[0].delete()
wb.sheets['Sheet 2'].range('I:I').insert()
wb.save('input_file.xlsm')

Upvotes: 0

Views: 2672

Answers (1)

Valerii
Valerii

Reputation: 114

As @moken already commented:

# import the lib
import xlwings as xw

# create a workbook
wb = xw.Book()

# for the first sheet (index 0) in range from A to A insert a column
wb.sheets[0].range('A:A').insert()

If you already have an xml file, you may open it with pandas:

import pandas as pd

# new dataframe
df = pd.read_xml("path.xml")

Then it is up to you how to manipulate with the data

Upvotes: 2

Related Questions