在没有Windows专用工具的情况下向现有xlsx添加密码(无人值守) [英] Add a password (unattended) to existing xlsx without Windows exclusive tools

查看:98
本文介绍了在没有Windows专用工具的情况下向现有xlsx添加密码(无人值守)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Openpyxl生成一个xlsx文件.而且,我想使用在同一脚本中作为变量的密码来保护工作簿本身.可以使用文件">密码" ..并在Excel本身中设置打开密码"来手动设置.

I'm generating an xlsx file using Openpyxl. And i'd like to protect the workbook itself using a password that I have as a variable in the same script. This can be set manually using File > Passwords.. and setting "Password to open" in Excel itself.

Openpyxl似乎仅通过ws.protection.set_password("mypassword")(其中ws是打开的工作表)提供基于工作表的编辑保护

Openpyxl only seems to offer sheet based edit protection through ws.protection.set_password("mypassword") (where ws is an open worksheet)

我似乎找不到确切的示例,但是我在某处看到xlsx文件基本上是zip存档,而当我运行unzip -t7z x之类的命令时似乎是正确的,但似乎使用实用程序添加了密码像7zzipcloak一样,当文件放回原处时,它会完全破坏文件.

I can't seem to find the exact examples but somewhere I read that xlsx files were basically zip archives, and while it seemed true when I ran commands like unzip -t and 7z x it seems that adding a password using utilities like 7z or zipcloak completely breaks the file when it's put back together.

 % 7z x ../sample.xlsx .

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64)

Scanning the drive for archives:
1 file, 98370 bytes (97 KiB)

Extracting archive: ../sample.xlsx
--
Path = ../sample.xlsx
Type = zip
Physical Size = 98370


No files to process
Everything is Ok

Files: 0
Size:       0
Compressed: 98370
 % 7z a -pmypassword sample.xlsx

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs x64)

Scanning the drive:
1 file, 6148 bytes (7 KiB)

Creating archive: sample.xlsx

Items to compress: 1


Files read from disk: 1
Archive size: 367 bytes (1 KiB)
Everything is Ok
 % open sample.xlsx

使用Excel打开时:

When opened with Excel:

Excel无法打开文件"sample.xlsx",因为文件格式为 文件扩展名无效.验证文件是否尚未 损坏,并且文件扩展名与文件格式匹配.

Excel cannot open the file 'sample.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

请注意,无论我对7z使用哪种类型,结果都相同,对于zipcloak也是如此.

Note the result is the same no matter which type I use with 7z, and the same with zipcloak too.

到目前为止,我已经使用Bash和Python查看了我的选择,这似乎很可怕.但是我几乎对任何事物都开放..我正在运行OS X和Debian的机器上.

So far i've looked at my options using Bash and Python, and it seems pretty dire. But i'm pretty much open to anything.. The machines i'm doing this on run OS X and Debian.

推荐答案

您要的内容当前在任何Python软件包中均不可用.您现在可能要做的最好的事情是安装以其他某种语言实现的软件包,然后从Python调用那个软件包(使用

What you're asking for isn't currently available in any Python package. The best you can probably do for now is to install a package implemented in some other language, and call that package from Python (using os.system() or the subprocess module or something along those lines).

我认识的两个人是

  • https://github.com/ankane/secure-spreadsheet (Node.js)
  • https://github.com/herumi/msoffice (C++)

secure-spreadsheet基本上是 xlsx-populate 的命令行包装.

secure-spreadsheet is basically a command-line wrapper for xlsx-populate.

似乎您希望能够在没有安装Excel的情况下执行此操作,但是为了完整起见,我会提到,如果您确实安装了Excel,那么另一种方法是使Excel本身自动化,这可以完成在Python中使用 xlwings 或它所依赖的基础包: appscript 在Mac上.

It seems like you want to be able to do this without having Excel installed, but for completeness I'll mention that if you do have Excel installed, then another way to do this is to automate Excel itself, which can be done in Python using xlwings, or the underlying packages that it depends upon: pywin32 on Windows or appscript on Mac.

这篇关于在没有Windows专用工具的情况下向现有xlsx添加密码(无人值守)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆