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

How to create a password protect copy of the excel file used with xlwings? #1568

Closed
Rachid70 opened this issue Apr 22, 2021 · 9 comments
Closed
Milestone

Comments

@Rachid70
Copy link

OS (e.g. Windows 10 or macOS Sierra)

Versions of xlwings, Excel and Python (e.g. 0.11.8, Office 365, Python 3.7)

Describe your issue (incl. Traceback!)

# Your traceback here

Include a minimal code sample to reproduce the issue (and attach a sample workbook if required!)

# Your code here
@fzumstein
Copy link
Member

At the moment, you would have to go via the ".api" workaround:

import xlwings as xw

book = xw.Book()
book.api.SaveAs(r'C:\path\to\withpassword.xlsx', Password='password')

@Rachid70
Copy link
Author

Yes it worked for me too. Thank you
Can I make the file read only in a similar way?

@fzumstein
Copy link
Member

Try this:

import xlwings as xw
from xlwings.constants import FileAccess

book = xw.books.active

book.api.ChangeFileAccess(Mode=FileAccess.xlReadOnly)

@evanrittenhouse
Copy link

Hi - would this be possible with MacOS? It uses appscript, not pywin32, so the api.SaveAs method doesn't work and I haven't been able to find an equivalent!

@fzumstein

@fzumstein
Copy link
Member

It's a bit harder to workaround on macOS due to the path handling, but it would be trivial to build it in. Basically, you'd have to provide the password argument here:

https://github.com/xlwings/xlwings/blob/master/xlwings/_xlmac.py#L321-L333

I'll try to include support for this in one of the next releases.

@fzumstein fzumstein added this to the 0.25.1 milestone Nov 16, 2021
@evanrittenhouse
Copy link

@fzumstein thank you so much!! I've been trying to do it myself, but got as far as trying to inject XML tags into the underlying .zip structure. When that still allowed read-only access, I gave up haha. I never knew encrypting a workbook would be so hard...

@vishalkumarpatel1610
Copy link

vishalkumarpatel1610 commented Jun 23, 2023

@fzumstein

`import xlwings as xw

book = xw.Book()
book.api.SaveAs(r'C:\path\to\withpassword.xlsx', Password='password')`

Do we have any function for this now? or still be using a workaround? Also what if I have already files and want to save as with a new name and make them password protected?

@fzumstein
Copy link
Member

@vishalkumarpatel1610
Copy link

vishalkumarpatel1610 commented Jun 23, 2023

@fzumstein it works fine - but it opens up an excel file right away after the execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants