I'm trying to modify an already existing file using openpyxl and python. I'm reading values from the file then doing a little programm and re-writing in the next column new values from the python programm.
My excel file is an xlsm file with macros working in it.
It looks like this in python :
import pandas as pd
from openpyxl import load_workbook
filepath = 'filepath.xlsm'
df = pd.read_excel(filepath, sheet_name="Sheet")
wb = load_workbook(filepath)
worksheet = wb['ER']
id = 10000000
j = 2
for row in df.iterrows():
values = (row[1]['Column1']).split(", ")
length = len(values)
for i in range(length):
values[i] = "ABC" str(id) "TEST"
id = id 1
final_values = str(values).replace("['", "").replace("'","").replace("]","")
worksheet.cell(row=j,column=2).value = final_values
j = j 1
wb.save(filepath)
The programm i am having is when i open the file again. I get the following error :
Excel Cannot Open the File Because The File Format or File Extension Is Not Valid
What do i need to change to be able to open the file correctly ?
CodePudding user response:
Try adding, keep_vba=True
wb = load_workbook('filepath.xlsm', keep_vba=True)
xlsm
is an extension that indicates that a file contains macros. This could be a reason for your problem.