You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Julia中读取受密码保护的Excel文件?附Python实现参考

读取受密码保护的Excel文件:Julia中的解决方案

目前Julia的原生Excel处理包(如XLSX.jl)暂不支持直接读取受密码保护的文件,但有两种可行的解决办法:

方法一:通过PyCall调用Python的msoffcrypto库

既然你已经熟悉Python的解密方案,可以直接在Julia中通过PyCall调用msoffcrypto完成解密,再用XLSX.jl读取解密后的内容。代码示例如下:

using DataFrames, XLSX, PyCall

# 导入Python模块
io = pyimport("io")
msoffcrypto = pyimport("msoffcrypto")

passwd = "xyz"
excel_path = "path/to/your/protected_file.xlsx"
sheet_name = "abc"

# 解密文件到内存中的BytesIO对象
decrypted_workbook = io.BytesIO()
open(excel_path, "rb") do file
    office_file = msoffcrypto.OfficeFile(file)
    office_file.load_key(password=passwd)
    office_file.decrypt(decrypted_workbook)
end

# 重置文件指针到起始位置
seekstart(decrypted_workbook)

# 读取为DataFrame
df = DataFrame(XLSX.readtable(decrypted_workbook, sheet_name)...)

方法二:使用LibreOffice命令行解密

如果你安装了LibreOffice,可以先用命令行工具将受保护的Excel文件转换为无密码版本,再用常规方式读取:

  1. 执行命令行解密(替换为你的文件路径和密码):
libreoffice --headless --convert-to xlsx:"Calc MS Excel 2007 XML" --password=xyz protected_file.xlsx
  1. 读取生成的无密码文件:
using DataFrames, XLSX

df = DataFrame(XLSX.readtable("protected_file.xlsx", "sheet_name")...)

注意事项

  • 原生Julia包(如XLSX.jl、ExcelFiles.jl)目前没有内置的密码解密功能,上述两种方法是当前的主流解决方案。
  • 使用PyCall时需要确保Python环境中已安装msoffcrypto库(可通过pip install msoffcrypto-tool安装)。

内容的提问来源于stack exchange,提问作者Darren Christopher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 06:22:49