Insert column with openpyxl

94 views Asked by At

I have searched and searched and I cannot find what is wrong with my script. I keep getting an error that openpyxl only works on certain types of file extensions. I am trying to add a column to an xlsx file. Here is my script. What is wrong here?

filename1 =r"C:\Users\user1\Documents\test.xlsx"
wb8 = openpyxl.load_workbook("filename1")
ws8 = wb8.worksheets["Sheet1"]
ws8.insert_cols(21)
wb8.save(str(filename1))
1

There are 1 answers

0
APhillips On BEST ANSWER

You are creating a filename1 variable that is the location of your Excel file. When you use it, you don't use a string version of it. Just call filename1.

filename1 =r"C:\Users\user1\Documents\test.xlsx"
wb8 = openpyxl.load_workbook(filename1) #just filename1
ws8 = wb8.worksheets["Sheet1"]
ws8.insert_cols(21)
wb8.save(filename1) #just filename1