简体   繁体   English

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

[英]Open password-protected ods file with python-xlswriter or bash

I need a code to open spreadsheet via python3 (preferred) or bash that I give them password and they read them.我需要一个代码来通过 python3(首选)或 bash 打开电子表格,我给他们密码并阅读它们。

I try by python-module "xlswriter"我尝试使用 python-module "xlswriter"

I protect xlsx with this method:我用这种方法保护 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')

or try with this code in bash-script或尝试在 bash-script 中使用此代码

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

But this does not return data in spreadsheet.但这不会在电子表格中返回数据。

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注意:以上代码在 python2.7 中运行良好

To open a password protected in terminal/bash you need a module which can be found here要在终端/bash 中打开受密码保护的密码,您需要一个可以在此处找到的模块

Worksheet protected and their attributes can be found here受保护的工作表及其属性可在此处找到

I protect xlsx with this method:我用这种方法保护 xlsx:

 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. XlsxWriter 不支持密码保护工作簿/xlsx 文件。

From the XlsxWriter docs on protect() :来自Protect() 上的 XlsxWriter 文档

Note笔记

Worksheet level passwords in Excel offer very weak protection. Excel 中的工作表级密码提供非常弱的保护。 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. XlsxWriter 不支持完整的工作簿加密,因为它需要完全不同的文件格式并且需要几个月的时间来实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM