使用 python-xlswriter 或 bash 打开受密码保护的 ods 文件

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

我需要一个代码来通过 python3(首选)或 bash 打开电子表格,我给他们密码,他们会阅读。

我尝试 python-模块 "xlswriter"

我用这个方法保护xlsx:


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')

或尝试在 bash-script

中使用此代码
LibreOffice -p password -f xlsx for-test-password.xlsx

但这不会 return 电子表格中的数据。

You missed the workbook.close()

找到代码。

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()

注意:以上代码在 python2.7

中运行良好

要打开受 terminal/bash 保护的密码,您需要一个可以找到的模块 here

工作表受保护,可以找到它们的属性here

I protect xlsx with this method:

worksheet.protect('Passwd')

这不是保护工作簿的密码。它正在保护工作表。 XlsxWriter 不支持密码保护 workbook/xlsx 文件。

来自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.