Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save and close an Excel file after adding data?

Tags:

python

excel

I am trying to open an existing Excel 2013 file, add data and then save it(same name) and then close it and then close Excel. The code will open the file, select the correct worksheet and write the data, but when I try save it I get an attribute error. Am I missing a library or something? Here is the code:

import win32com.client as win32

def Inventory_Status():
    excel = win32.gencache.EnsureDispatch('Excel.Application') # opens Excel
    wb = excel.Workbooks.Open(r'C:/pytest/Test.xlsx') # opens "Test" file
    wb.Sheets(2).Select() # select 2nd worksheet "Aisle_2"
    excel.Visible = True
    excel.Range("A1").Select()
    excel.ActiveCell.Value = "1234"   # Fill in test data #
    wb.save()
    wb.Close()
    excel.Quit()

Inventory_Status()

raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))

AttributeError: '<win32com.gen_py.Microsoft Excel 15.0 Object Library._Workbook instance at 0x5901424>' object has no attribute 'save'
like image 295
JeffC Avatar asked Jul 28 '15 15:07

JeffC


People also ask

How do I close Excel without losing data?

Click Save on the left-hand pane of the Excel Options dialog. Make sure that both Save AutoRecover information every X minutes and Keep the last autosaved version if I close without saving are checked. Click OK. By default the AutoRecover feature is set to automatically save changes to your workbook every 10 minutes.

How do I AutoSave Excel data after entering?

Close and re-open the file from within the Office app, not the recent file list. AutoSave settings may be disabled for some files, especially large files, or files stored on SharePoint. Go to File > Options > Save. Check that the AutoSave box is ticked.

How do I automatically save and close workbook after inactivity in Excel?

Enable the workbook you want to automatically save and close after inactivity for a certain seconds, and press Alt + F11 keys to open Microsoft Visual Basic for Applications window. 5. Then after 15 seconds, there is a dialog popping out for remind you saving the workbook, and click Yes to save and close the workbook.


1 Answers

Capitalize the 's' on the save() method.

like image 108
Alecg_O Avatar answered Oct 08 '22 01:10

Alecg_O