import os import glob from lxml import etree as ET # Directory path input_directory = os.path.join(os.getcwd(), 'input') output_directory = os.path.join(os.getcwd(), 'output') # Create output directory if it doesn't exist os.makedirs(output_directory, exist_ok=True) # Remove all existing files in output directory for filename in os.listdir(output_directory): os.remove(os.path.join(output_directory, filename)) # Get all XML files in the input directory files = glob.glob(os.path.join(input_directory, '*.xml')) ns = {'ns': 'http://openlyrics.info/namespace/2009/song'} for file in files: # Parse XML file parser = ET.XMLParser(remove_blank_text=True) tree = ET.parse(file, parser) root = tree.getroot() # Prepare output output = "Title(s):\n" titles = root.findall('.//ns:title', ns) for title in titles: output += (title.text or '').strip() + "\n" copyright = root.find('.//ns:copyright', ns) if copyright is not None and copyright.text: output += "Copyright: " + copyright.text.strip() + "\n" verseOrder = root.find('.//ns:verseOrder', ns) if verseOrder is not None and verseOrder.text: output += "Verse Order: " + verseOrder.text.strip() + "\n" ccliNo = root.find('.//ns:ccliNo', ns) if ccliNo is not None and ccliNo.text: output += "CCLI No.: " + ccliNo.text.strip() + "\n" authors = root.findall('.//ns:author', ns) if authors: output += "Author(s): " for i, author in enumerate(authors): output += (author.text or '').strip() if i < len(authors) - 1: output += ", " else: output += "\n" output += "Lyrics:\n" verses = root.findall('.//ns:verse', ns) for verse in verses: lines_in_verse = verse.findall('.//ns:lines', ns) for lines in lines_in_verse: if lines is not None: lines_text = ET.tostring(lines, method='text', encoding='utf-8').decode('utf-8') lines_text = lines_text.replace('
', '\n').strip() lines_text = ' '.join(lines_text.split()) output += lines_text + "\n" output += "\n" # Write output to a text file in output directory txt_file_name = os.path.join(output_directory, os.path.splitext(os.path.basename(file))[0] + '.txt') with open(txt_file_name, 'w') as f: f.write(output) print("All files have been processed and saved!")