从XLSM文件中用Python提取VBA为TXT。

huangapple go评论125阅读模式
英文:

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

  1. #pip install oletools
  2. import oletools.olevba as olevba
  3. vba = olevba.VBA_Parser("file.xlsm")
  4. with open("output.txt", "w") as f:
  5. for *_, n, m in vba.extract_all_macros():
  6. if n.startswith("Module"):
  7. print(m, file=f)

注意:如果你想要同时针对Excel对象进行操作,你可以使用这种方法

输出 (output.txt):

  1. Attribute VB_Name = "Module1"
  2. Sub Foo()
  3. Range("B1").Select
  4. ActiveCell.FormulaR1C1 = "StackOverflow"
  5. Range("B2").Select
  6. End Sub

使用的输入 (file.xlsm) 包含这个代码:

从XLSM文件中用Python提取VBA为TXT。

英文:

You can use olevba :

  1. #pip install oletools
  2. import oletools.olevba as olevba
  3. vba = olevba.VBA_Parser("file.xlsm")
  4. with open("output.txt", "w") as f:
  5. for *_, n, m in vba.extract_all_macros():
  6. if n.startswith("Module"):
  7. print(m, file=f)

NB : If you want to target the Excel objects as well, you can use this approach.

Output (output.txt) :

  1. Attribute VB_Name = "Module1"
  2. Sub Foo()
  3. Range("B1").Select
  4. ActiveCell.FormulaR1C1 = "StackOverflow"
  5. Range("B2").Select
  6. End Sub

Input used (file.xlsm) with this code :

从XLSM文件中用Python提取VBA为TXT。

huangapple
  • 本文由 发表于 2023年6月15日 18:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定