通过Python导出Excel模块

时间:2018-03-01 08:06:35

标签: python excel vba export win32com

我试图在Python中复制从Excel工作表中导出代码模块。

以下适用于VBA:

Public Sub ExportModules()
    Dim wb As Workbook
    Set wb = ThisWorkbook
    Dim D As String
    Dim N

    D = ThisWorkbook.Path
    For Each VBComp In wb.VBProject.VBComponents
        If (VBComp.Type = 1) Then
            N = D + "\" + VBComp.Name + ".txt"
            VBComp.Export N
        End If
    Next
End Sub

我在Python中有以下内容:

import os
import sys
import glob
from win32com.client import Dispatch

scripts_dir = 'folder address'

com_instance = Dispatch("Excel.Application")
com_instance.Visible = False
com_instance.DisplayAlerts = False

for script_file in glob.glob(os.path.join(scripts_dir, "*.xlsm")):
    print "Processing: %s" % script_file
    (file_path, file_name) = os.path.split(script_file)
    objworkbook = com_instance.Workbooks.Open(script_file)
    for xlmodule in objworkbook.VBProject.VBComponents:
        xlmodule.Export('export file name')

我的问题是,我在Python中需要做什么来根据VBA代码复制文件的导出?

1 个答案:

答案 0 :(得分:0)

使用默认的oletools xltrails提供了一种从.bas或其他excel文件中提取.xlsm文件的好方法

import os
import shutil
from oletools.olevba3 import VBA_Parser


EXCEL_FILE_EXTENSIONS = ('xlsb', 'xls', 'xlsm', 'xla', 'xlt', 'xlam',)


def parse(workbook_path):
    vba_path = workbook_path + '.vba'
    vba_parser = VBA_Parser(workbook_path)
    vba_modules = vba_parser.extract_all_macros() if vba_parser.detect_vba_macros() else []

    for _, _, _, content in vba_modules:
        decoded_content = content.decode('latin-1')
        lines = []
        if '\r\n' in decoded_content:
            lines = decoded_content.split('\r\n')
        else:
            lines = decoded_content.split('\n')
        if lines:
            name = lines[0].replace('Attribute VB_Name = ', '').strip('"')
            content = [line for line in lines[1:] if not (
                line.startswith('Attribute') and 'VB_' in line)]
            if content and content[-1] == '':
                content.pop(len(content)-1)
                lines_of_code = len(content)
                non_empty_lines_of_code = len([c for c in content if c])
                if non_empty_lines_of_code > 0:
                    if not os.path.exists(os.path.join(vba_path)):
                        os.makedirs(vba_path)
                    with open(os.path.join(vba_path, name + '.bas'), 'w') as f:
                        f.write('\n'.join(content))


if __name__ == '__main__':
    for root, dirs, files in os.walk('.'):
        for f in dirs:
            if f.endswith('.vba'):
                shutil.rmtree(os.path.join(root, f))

        for f in files:
            if f.endswith(EXCEL_FILE_EXTENSIONS):
                parse(os.path.join(root, f))

我已经尝试过了,效果很好。

参考https://www.xltrail.com/blog/auto-export-vba-commit-hook