英文:
Extract VBA as TXT from XLSM with Python
问题
我想从一个.xlsm
文件中提取VBA
代码,并将其保存为.txt
文件。不幸的是,当我解压缩.xlsm
文件时,我只得到.bin
文件,无法用编辑器打开。
英文:
I want to extract the VBA
code from an .xlsm
file and save it as .txt
files. Unfortunately, I get only .bin
files when I unzip the .xlsm
file which I cannot open with an editor.
答案1
得分: 1
你可以使用 olevba
:
#pip install oletools
import oletools.olevba as olevba
vba = olevba.VBA_Parser("file.xlsm")
with open("output.txt", "w") as f:
for *_, n, m in vba.extract_all_macros():
if n.startswith("Module"):
print(m, file=f)
注意:如果你想要同时针对Excel对象进行操作,你可以使用这种方法。
输出 (output.txt
):
Attribute VB_Name = "Module1"
Sub Foo()
Range("B1").Select
ActiveCell.FormulaR1C1 = "StackOverflow"
Range("B2").Select
End Sub
使用的输入 (file.xlsm
) 包含这个代码:
英文:
You can use olevba
:
#pip install oletools
import oletools.olevba as olevba
vba = olevba.VBA_Parser("file.xlsm")
with open("output.txt", "w") as f:
for *_, n, m in vba.extract_all_macros():
if n.startswith("Module"):
print(m, file=f)
NB : If you want to target the Excel objects as well, you can use this approach.
Output (output.txt
) :
Attribute VB_Name = "Module1"
Sub Foo()
Range("B1").Select
ActiveCell.FormulaR1C1 = "StackOverflow"
Range("B2").Select
End Sub
Input used (file.xlsm
) with this code :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论