我有一大组.xls
(Excel 97-2003 工作簿)文件。其中有一些里面包含VBA宏,我想找到一种方法可以自动过滤掉它们,而无需在MS Excel中逐个打开它们。
有一个old post这与我的问题类似,我已经下载了 OLE Doc viewer ,但是打不开.zip文件,好像已经过时了...
有谁知道是否有任何 API 或工具可以检查 .xls
文件是否包含 VBA 宏而无需在 MS Excel 中打开它?首先,我懒得去了解宏的内容。
PS:对于.xlsx
或.xlsm
文件,我们可以将其文件扩展名更改为.zip
,其中包含一些 .xml
文件以及最终用于 VBA 宏的 vbaProject.bin
。但是,此方法不适用于 .xls
文件,重命名它不会生成有效的 .zip
文件。
最佳答案
这是一个简单的 Python 2.7我为你准备的解决方案:
它仅取决于可用的 OleFileIO_PL 模块 from the project page OleFile 解析器的好处是它不知道文件的“特定于 Excel”的内容;它只知道更高级别的“OLE存储”。因此,分析文件的速度很快,并且不存在执行潜在有害宏的风险。
import OleFileIO_PL
import argparse
if __name__=='__main__':
parser = argparse.ArgumentParser(description='Determine if an Excel 97-2007 file contains macros.', epilog='Will exit successfully (0) only if provided file is a valid excel file containing macros.')
parser.add_argument('filename', help="file name to analyse")
args=parser.parse_args()
# Test if a file is an OLE container:
if (not OleFileIO_PL.isOleFile(args.filename)):
exit("This document is not a valid OLE document.")
# Open an OLE file:
ole = OleFileIO_PL.OleFileIO(args.filename)
# Test if known streams/storages exist:
if (not ole.exists('workbook')):
exit("This document is not a valid Excel 97-2007 document.")
# Test if VBA specific streams exist:
if (not ole.exists('_VBA_PROJECT_CUR')):
exit("This document does not contain VBA macros.")
print("Valid Excel 97-2007 workbook WITH macros")
我在几个文件上进行了测试,并取得了成功。让我知道它是否适合您
关于vba - 验证 xls 文件是否包含 VBA 宏,而无需在 MS Excel 中打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17550648/