Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac: update wb reference when saving as #626

Closed
ahmedelz opened this issue Feb 14, 2017 · 3 comments · Fixed by #1372
Closed

Mac: update wb reference when saving as #626

ahmedelz opened this issue Feb 14, 2017 · 3 comments · Fixed by #1372

Comments

@ahmedelz
Copy link

Hi,

I'm trying to save the sheet as .xlsb on Mac OS X.. I have this piece of code:

wb = xw.Book(fullname=fname)
.....
# lots of code that increases the workbook file size significantly
....
if sys.platform.startswith('darwin'):
    wb.save()
    wb.api.save_workbook_as(filename=xw._xlmac.posix_to_hfs_path(fname[:-5] + '.xlsb'), file_format=xw.constants.FileFormat.xlExcel12, overwrite=True)
wb.close()

However, this seems to fail in appscript on the save_workbook_as line with the following piece of the traceback:

    wb.api.save_workbook_as(filename=xw._xlmac.posix_to_hfs_path(fname[:-5] + '.xlsb'), file_format=xw.constants.FileFormat.xlExcel12, overwrite=True)
  File "/Library/Python/2.7/site-packages/aeosa/appscript/reference.py", line 498, in __call__
    raise CommandError(self, (args, kargs), e, self.AS_appdata)
appscript.reference.CommandError: Command failed:
          OSERROR: -50
          MESSAGE: Parameter error.
          COMMAND: app(pid=29029).workbooks[....].save_workbook_as(file_format=50, overwrite=True, filename=u'.....xlsb')

The executed command seems to be what I was expecting but not sure why it is failing. Any suggestions or hints?

@ahmedelz
Copy link
Author

ahmedelz commented Feb 14, 2017

I think I found the explanation here: http://www.rondebruin.nl/mac/mac020.htm. It seems that the 'constants' are not the same on Mac vs. Windows and looks like I need to add 1.

So, the offending line needs to be updated to:
wb.api.save_workbook_as(filename=xw._xlmac.posix_to_hfs_path(fname[:-5] + '.xlsb'), file_format=(xw.constants.FileFormat.xlExcel12 + 1), overwrite=True)

But then it fails on the wb.close:

  File "/Library/Python/2.7/site-packages/aeosa/appscript/reference.py", line 498, in __call__
    raise CommandError(self, (args, kargs), e, self.AS_appdata)
appscript.reference.CommandError: Command failed:
		OSERROR: -1728
		MESSAGE: The object you are trying to access does not exist
		COMMAND: app(pid=48472).workbooks['....xlsx'].close(saving=k.no)

wb is still referring to the original workbook that was opened (.xlsx) so I'm not sure why the error says it doesn't exist.

@fzumstein
Copy link
Member

Although the behaviour might be improved, it's kind of expected: your old workbook isn't open anymore after a save_workbook_as, so it cannot be closed.
Currently you'd need to reinstantiate a Book object with the new workbook.

@fzumstein fzumstein added the bug label Feb 19, 2017
@fzumstein fzumstein added this to the short list milestone Feb 19, 2017
@fzumstein fzumstein changed the title Appscript command error when trying to save as .xlsb Mac: update wb reference when saving as Feb 19, 2017
@fzumstein
Copy link
Member

Actually you're right we should fix this.

@fzumstein fzumstein modified the milestones: short list, v0.13.1 Oct 20, 2018
@fzumstein fzumstein modified the milestones: v0.14.0, shortshortlist Nov 4, 2018
@fzumstein fzumstein removed this from the shortshortlist milestone Aug 11, 2019
@fzumstein fzumstein added this to the 0.20.1 milestone Aug 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants