import zipfile
import xml.etree.ElementTree as ET
import sys
import json

def extract_text_from_docx(docx_path):
    try:
        document = zipfile.ZipFile(docx_path)
        xml_content = document.read('word/document.xml')
        document.close()
        
        tree = ET.XML(xml_content)
        WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
        PARA = WORD_NAMESPACE + 'p'
        TEXT = WORD_NAMESPACE + 't'
        
        paragraphs = []
        for paragraph in tree.iter(PARA):
            texts = [node.text for node in paragraph.iter(TEXT) if node.text]
            if texts:
                paragraphs.append(''.join(texts))
                
        return '\n'.join(paragraphs)
    except Exception as e:
        return str(e)

if __name__ == '__main__':
    path = sys.argv[1]
    text = extract_text_from_docx(path)
    with open('c:\\Users\\Islam\\Documents\\Dubai-South-lp\\extracted_content.txt', 'w', encoding='utf-8') as f:
        f.write(text)
    print("Extraction successful. Saved to extracted_content.txt")
