这是我为您准备的一个简单的Python 2.7解决方案:
它只依赖于项目页面中可用的 OleFileIO_PL 模块
OleFile 解析器的好处是它不知道文件的“excel-specific”内容;它只知道更高级别的“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")
我成功地在几个文件上测试了它。让我知道它是否适合你