Open password-protected ods file with python-xlswriter or bash

只愿长相守 提交于 2021-01-29 10:54:05

问题


I need a code to open spreadsheet via python3 (preferred) or bash that I give them password and they read them.

I try by python-module "xlswriter"

I protect xlsx with this method:


import xlsxwriter
workbook = xlsxwriter.Workbook('for-test-password.xlsx')
worksheet = workbook.add_worksheet()
content = (
    ['Gas', 10],
    ['Gasoline',   20],
    ['Potro',  30],
    ['Other',40],
)

row = 0
col = 0
for item, cost in (content):
    worksheet.write(row, col,     item)
    worksheet.write(row, col + 1, cost)
    row += 1
worksheet.protect('Passwd')

or try with this code in bash-script

LibreOffice -p password -f xlsx for-test-password.xlsx

But this does not return data in spreadsheet.


回答1:


I protect xlsx with this method:

worksheet.protect('Passwd')

This is not password protecting the workbook. It is protecting the worksheet. There is no support in XlsxWriter for password protecting a workbook/xlsx file.

From the XlsxWriter docs on protect():

Note

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.




回答2:


You missed the workbook.close()

Find the code.

import xlsxwriter
workbook = xlsxwriter.Workbook('for-test-password.xlsx')
worksheet = workbook.add_worksheet()
content = (['Gas', 10],['Gasoline', 20],['Potro', 30],['Other',40],)
row = 0
col = 0
for item, cost in (content):
    worksheet.write(row, col, item)
    worksheet.write(row, col + 1, cost)
    row += 1
    worksheet.protect('Passwd')
workbook.close()

Note: Above code is working fine in python2.7

To open a password protected in terminal/bash you need a module which can be found here

Worksheet protected and their attributes can be found here



来源:https://stackoverflow.com/questions/58705829/open-password-protected-ods-file-with-python-xlswriter-or-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!