sirhzx 2022-02-07 16:27 采纳率: 0%
浏览 189

Mac系统,在jupyter中使用xlwings建立exce表后,用了wb.close()之后使用wb.save()会报错,反之亦然,两者只能输入一个

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
import xlwings as xw
import time
app=xw.App(visible=False)
wb=app.books.open('data.xlsx')
wb=app.books.add()
ws=wb.sheets.add('t')
ws=wb.sheets[0]
ws.range('a1').value='hzx'

wb.close()
time.sleep(3)
wb.save(r'/Users/houzhixian/Desktop/phzx21.xlsx')
# app.quit()

运行结果及报错内容

EventError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.9/site-packages/aeosa/appscript/reference.py in call(self, *args, **kargs)
481 try:
--> 482 return self.AS_appdata.target().event(self._code, params, atts, codecs=self.AS_appdata).send(timeout, sendflags)
483 except aem.EventError as e:

~/opt/anaconda3/lib/python3.9/site-packages/aeosa/aem/aemsend.py in send(self, timeout, flags)
91 errormsg = _defaultcodecs.unpack(errormsg)
---> 92 raise EventError(errornum, errormsg, eventresult)
93 if kae.keyAEResult in eventresult: # application has returned a value

EventError: Command failed: The object you are trying to access does not exist (-1728)

The above exception was the direct cause of the following exception:

CommandError Traceback (most recent call last)
/var/folders/tv/dt3jjn2s4j33tvvvld49v6lh0000gn/T/ipykernel_1238/961393794.py in
10 wb.close()
11 time.sleep(3)
---> 12 wb.save(r'/Users/houzhixian/Desktop/phzx21.xlsx')
13 # app.quit()

~/opt/anaconda3/lib/python3.9/site-packages/xlwings/main.py in save(self, path)
872 path = utils.fspath(path)
873 with self.app.properties(display_alerts=False):
--> 874 self.impl.save(path)
875
876 @property

~/opt/anaconda3/lib/python3.9/site-packages/xlwings/_xlmac.py in save(self, path)
293
294 def save(self, path):
--> 295 saved_path = self.xl.properties().get(kw.path)
296 source_ext = os.path.splitext(self.name)[1] if saved_path else None
297 target_ext = os.path.splitext(path)[1] if path else '.xlsx'

~/opt/anaconda3/lib/python3.9/site-packages/aeosa/appscript/reference.py in call(self, *args, **kargs)
662
663 def call(self, *args, **kargs):
--> 664 return self.get(*args, **kargs)
665
666 # Following methods will be called by its-based generic references when resolving themselves into real references; end-users and other clients shouldn't call them directly.

~/opt/anaconda3/lib/python3.9/site-packages/aeosa/appscript/reference.py in call(self, *args, **kargs)
516 return
517 raise CommandError(self, (args, kargs), e, self.AS_appdata) from e
--> 518 raise CommandError(self, (args, kargs), e, self.AS_appdata) from e
519
520 def AS_formatcommand(self, args):

CommandError: Command failed:
OSERROR: -1728
MESSAGE: The object you are trying to access does not exist
COMMAND: app('/Applications/Microsoft Excel.app', newinstance=(0, 782527)).workbooks['工作簿3'].properties.get()

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • 「已注销」 2022-02-07 18:00
    关注

    顺序反了,wb对象已经被你用wb.close()关闭了,所以不存在了,再用wb.save()就会报错。
    先save再close

    评论

报告相同问题?

问题事件

  • 创建了问题 2月7日

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,