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))
def parse(workbook_path): # vba_path = 'src.vba' 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 _, _, filename, content in vba_modules: # decoded_content = content.decode('latin-1') decoded_content = content lines = [] if '\r\n' in decoded_content: lines = decoded_content.split('\r\n') else: lines = decoded_content.split('\n') if lines: content = [] for line in lines: if line.startswith('Attribute') and 'VB_' in line: if 'VB_Name' in line and KEEP_NAME: content.append(line) else: content.append(line) if content and content[-1] == '': content.pop(len(content) - 1) 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, filename), 'w', encoding='utf-8') as f: f.write('\n'.join(content))
def get_vba(workbook): vba_parser = VBA_Parser(workbook) vba_modules = vba_parser.extract_all_macros() if vba_parser.detect_vba_macros() else [] modules = {} 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)] non_empty_lines_of_code = len([c for c in content if c]) modules[name] = '\n'.join(content) return modules