添加数据后保存并关闭Excel文件? [英] Save and close an Excel file after adding data?

查看:330
本文介绍了添加数据后保存并关闭Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开现有的Excel 2013文件,添加数据,然后保存(相同名称),然后将其关闭,然后关闭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'

推荐答案

使用save()方法将 's' 大写.

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

这篇关于添加数据后保存并关闭Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆