Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save changes with openpyxl

I am new to python and an using openpyxl to edit an xlsx file. I am having an issue trying to save the original file. It seems that openpyxl keeps making me save the changes as a new xlsx.

Here is the code I am using and get the error TypeError: save() takes exactly 2 arguments (1 given)

import openpyxl
from openpyxl import Workbook
wb = openpyxl.load_workbook('book1.xlsx')
sheet = wb.get_sheet_by_name("Sensor Status")
sheet['I3'] = '=countifs(B:B,"*server*",C:C,"=0")'
sheet['I4'] = '=countifs(B:B,"*server*",C:C,">=0")'
wb.save()
like image 404
Lord Beerus Avatar asked Oct 30 '25 05:10

Lord Beerus


2 Answers

Sir,

You need to add the file name like:

wb.save('book1.xlsx')
like image 129
Gen Wan Avatar answered Oct 31 '25 19:10

Gen Wan


Gen Wan's answer is already correct. But, assuming you already did that and you still had an error, this might help you. I had the same problem, and I figured out that it gave me an error because my file was still open in Microsoft Excel while I was trying to save it with openpyxl. When the one same file is opened by two platforms (Which in this case are Microsoft Excel and openpyxl), I think that the priviledge to save the file is prioritized for the Microsoft Excel software, that's why it's declining the save command from openpyxl. Once I closed Microsoft Excel, I no longer had the error and I was able to save the file. I am assuming you had the error because of that too.

like image 43
athenax17 Avatar answered Oct 31 '25 19:10

athenax17