问题描述
我正在尝试使用密码保存加密的Excel文件。我已经尝试遵循的指南 - 并且工作完美。但是,这是在GUI中,但是我正在寻找一种使用无头模式的命令行界面的解决方案。
我看过 man libreoffice
,但我找不到任何东西。
同样,我已经看过Python 3库的文档 openpyxl
,但是我没有找到任何有用的东西
是否可以使用命令行(或Python)在Ubuntu 14.04 / 16.04上使用密码将密码加密的Excel 2007+文件保存为库)不需要任何用户交互或X会话?
有解决方案使用和。如果想要使用CPython / PyPy,可以使用模块调用外部Jython脚本。
- 我假设你安装了Java JRE / JDK
- 创建使用Excel / Calc的非加密 xlsx文件,或使用或并保存为 test1.xlsx
- 下载独立的Jython
- 下载Apache POI
- 在同一个目录中提取Apache POI,其中独立的Jython jar
- 将以下Jython脚本另存为 encrypt.py :
import os
import java.io导入sys
BufferedInputStream
从java.io导入FileInputStream
from java.io import FileOutputStream
from java.io im端口File
from java.io import IOException
from org.apache.poi.poifs.crypt import EncryptionInfo,EncryptionMode
from org.apache.poi.poifs.crypt import CipherAlgorithm,HashAlgorithm $ b来自org.apache.poi.poifs.crypt.agile的$ b从org.apache.poi.openxml4j.opc导入AgileEncryptionInfoBuilder
import OPCPackage,来自org.apache.poi.poifs.filesystem导入的PackageAccess
导入POIFSFileSystem
from org.apache.poi.ss.usermodel import WorkbookFactory
def encrypt_xlsx(in_fname,out_fname,password):
#read
in_f = File(in_fname)
in_wb = WorkbookFactory.create(in_f,password)
in_fis = FileInputStream(in_fname)
in_wb.close()
#加密
out_poi_fs = POIFSFileSystem ()
info = EncryptionInfo(EncryptionMode.agile)
enc = info.getEncryptor()
enc.confirmPassword(password)
opc = OPCPackage.open(in_f,PackageAccess.READ_WRITE )
out_os = enc.getDataStream(out_ poi_fs)
opc.save(out_os)
opc.close()
#write
out_fos = FileOutputStream(out_fname)
out_poi_fs.writeFilesystem(out_fos )
out_fos.close()
如果__name__ =='__main__':
in_fname = sys.argv [1]
out_fname = sys.argv [2]
password = sys.argv [3]
encrypt_xlsx(in_fname,out_fname,password)
java -cpjython-standalone-2.7.0.jar:poi-3.15 / lib / commons-codec-1.10.jar:poi-3.15 / lib / commons-collections4-4.1.jar :POI-3.15 / POI-3.15.jar:POI-3.15 / POI-OOXML-3.15.jar:POI-3.15 / POI-OOXML-架构 - 3.15.jar:POI-3.15 / OOXML-LIB / curvesapi-1.04.jar :poi-3.15 / ooxml-lib / xmlbeans-2.6.0.jarorg.python.util.jython -B encrypt.py test1.xlsx test1enc.xlsx 12345678
其中:
- encrypt.py - 脚本名称
- test1.xlsx - 输入文件名
- test1enc.xlsx - 输出文件名
- 12345678 - 密码
最终加密 xslx应该在test1enc.xlsx 中。
I am trying to save an Excel file encrypted with password. I have tried following the guide on https://help.libreoffice.org/Common/Protecting_Content_in - and works perfectly. However, this is in the GUI, but I am looking for a solution using the command line interface in headless mode.
I have looked at the man libreoffice
, but I could not find anything in there.
Likewise I have looked at the documentation of the Python 3 library openpyxl
, but I did not find anything useful there either.
Is it possible to save an Excel 2007+ file encrypted with a password on Ubuntu 14.04/16.04 using the command line (or Python library) that do not require any user interaction or X session?
There is solution using Jython and Apache POI. If you want like to use it from CPython/PyPy, you can use subprocess module to call external Jython script.
- I assume that you have Java JRE/JDK installed
- Create non-encrypted xlsx file with Excel/Calc or use xlsxwriter or openpyxl and save it as test1.xlsx
- Download standalone Jython
- Download Apache POI
- Extract Apache POI in same dir where is standalone Jython jar
- Save following Jython script as encrypt.py:
import os
import sys
from java.io import BufferedInputStream
from java.io import FileInputStream
from java.io import FileOutputStream
from java.io import File
from java.io import IOException
from org.apache.poi.poifs.crypt import EncryptionInfo, EncryptionMode
from org.apache.poi.poifs.crypt import CipherAlgorithm, HashAlgorithm
from org.apache.poi.poifs.crypt.agile import AgileEncryptionInfoBuilder
from org.apache.poi.openxml4j.opc import OPCPackage, PackageAccess
from org.apache.poi.poifs.filesystem import POIFSFileSystem
from org.apache.poi.ss.usermodel import WorkbookFactory
def encrypt_xlsx(in_fname, out_fname, password):
# read
in_f = File(in_fname)
in_wb = WorkbookFactory.create(in_f, password)
in_fis = FileInputStream(in_fname)
in_wb.close()
# encryption
out_poi_fs = POIFSFileSystem()
info = EncryptionInfo(EncryptionMode.agile)
enc = info.getEncryptor()
enc.confirmPassword(password)
opc = OPCPackage.open(in_f, PackageAccess.READ_WRITE)
out_os = enc.getDataStream(out_poi_fs)
opc.save(out_os)
opc.close()
# write
out_fos = FileOutputStream(out_fname)
out_poi_fs.writeFilesystem(out_fos)
out_fos.close()
if __name__ == '__main__':
in_fname = sys.argv[1]
out_fname = sys.argv[2]
password = sys.argv[3]
encrypt_xlsx(in_fname, out_fname, password)
- Call it from console:
java -cp "jython-standalone-2.7.0.jar:poi-3.15/lib/commons-codec-1.10.jar:poi-3.15/lib/commons-collections4-4.1.jar:poi-3.15/poi-3.15.jar:poi-3.15/poi-ooxml-3.15.jar:poi-3.15/poi-ooxml-schemas-3.15.jar:poi-3.15/ooxml-lib/curvesapi-1.04.jar:poi-3.15/ooxml-lib/xmlbeans-2.6.0.jar" org.python.util.jython -B encrypt.py test1.xlsx test1enc.xlsx 12345678
Where:
- encrypt.py - name of script
- test1.xlsx - input filename
- test1enc.xlsx - output filename
- 12345678 - password
Final encrypted xslx should be in test1enc.xlsx.
这篇关于Python或LibreOffice保存密码加密的xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!