import xml.etree.ElementTree as ET
import os

def extract_text_from_xml(xml_path):
    if not os.path.exists(xml_path):
        return "File not found"
    
    tree = ET.parse(xml_path)
    root = tree.getroot()
    
    # Define the namespace for WordprocessingML
    ns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
    
    texts = []
    for p in root.findall('.//w:p', ns):
        paragraph_text = ""
        for t in p.findall('.//w:t', ns):
            if t.text:
                paragraph_text += t.text
        if paragraph_text:
            texts.append(paragraph_text)
    
    return "\n".join(texts)

if __name__ == "__main__":
    xml_file = "temp_docx/word/document.xml"
    content = extract_text_from_xml(xml_file)
    with open("scratch/guide_content.txt", "w", encoding="utf-8") as f:
        f.write(content)
    print("Content extracted to scratch/guide_content.txt")
