Reputation: 1432
I'm trying to use xlsxwriter to create an xlsx file with a workbook password. I glanced through the documentation and skimmed the source code, but can't seem to find anything about it.
Upvotes: 2
Views: 5102
Reputation: 97
Found the perfect solution to the problem. Doesn't exactly use xlsxwriter but works flawlessly. Uses pywin32, so sadly limited to windows machines only.
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
#Before saving the file set DisplayAlerts to False to suppress the warning dialog:
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(your_file_name)
# refer https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb214129(v=office.12)?redirectedfrom=MSDN
# FileFormat = 51 is for .xlsx extension
wb.SaveAs(your_file_name, 51, 'your password')
wb.Close()
excel.Application.Quit()
Here's a link to the original answer.
Upvotes: 1
Reputation: 61
The worksheet.protect() only protect the worksheet from being edited. But, we can combine the protect() method with hidden format to hide the data from first view. Only if you have the password can you unlock and unhidden the cells.
Here is my approach:
import xlsxwriter
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
test_content = (
['Hi', 123],
['I', 1123],
['AM', 1231231],
['LOCKED',1123123],
)
row = 0
col = 0
for text, num in (test_content):
worksheet.write(row, col, text)
worksheet.write(row, col + 1, num)
worksheet.set_row(row, None, None, {'hidden': True}) #hide the row
row += 1
worksheet.protect('password') # set the password and protect
workbook.close()
Upvotes: 2
Reputation: 4130
according to the documentation here
Worksheet level passwords in Excel offer very weak protection. They do not encrypt your data and are very easy to deactivate. Full workbook encryption is not supported by XlsxWriter since it requires a completely different file format and would take several man months to implement.
you can add password to the worksheet,but it is not a strong protected method
worksheet.protect(<password>)
Upvotes: 1
Reputation: 521
Are you try setting the password like this:
wb = Workbook()
ws = wb.worksheets[0]
ws.protect('abc123')
Upvotes: 0