Below is a ready-to-run Python snippet that will:
• walk through every .aspx file inside the folder justify
• strip all HTML, keeping a real newline only where a <br> (or <br/>) tag appears
• write the cleaned text to a mirror directory clean, switching the extension to .txt
Feel free to tweak the list NEWLINE_TAGS (e.g., add 'p', 'li', etc.) if you want those tags to trigger line-breaks as well.
# clean_aspx_to_txt.py
# --------------------
# Requires: pip install beautifulsoup4
import os, re, pathlib
from bs4 import BeautifulSoup
SRC_DIR = pathlib.Path('justify')
DEST_DIR = pathlib.Path('clean')
EXT_IN = '.aspx'
EXT_OUT = '.txt'
DEST_DIR.mkdir(exist_ok=True)
# Which tags should become real new-lines in the output?
NEWLINE_TAGS = {'br'} # add 'p', 'li', ... if desired
PLACEHOLDER = '<<<NL>>>' # unlikely string to mark line breaks during cleanup
def html_to_plaintext(html: str) -> str:
# 1) Put a placeholder after every newline-worthy tag
for tag in NEWLINE_TAGS:
html = re.sub(fr'<\s*{tag}\s*/?>', PLACEHOLDER, html, flags=re.I)
# 2) Strip ALL markup with BeautifulSoup
text = BeautifulSoup(html, 'html.parser').get_text()
# 3) Collapse runs of whitespace (including original newlines)
text = re.sub(r'\s+', ' ', text)
# 4) Convert placeholders back to real newlines
text = text.replace(PLACEHOLDER, '\n').strip()
return text
def main():
for path in SRC_DIR.rglob(f'*{EXT_IN}'):
clean_text = html_to_plaintext(path.read_text(encoding='utf-8', errors='ignore'))
# Mirror the directory tree inside DEST_DIR
rel_path = path.relative_to(SRC_DIR).with_suffix(EXT_OUT)
out_path = DEST_DIR / rel_path
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(clean_text, encoding='utf-8')
print(f'✔︎ {path} → {out_path}')
if __name__ == '__main__':
main()
How to run
pip install beautifulsoup4 (if you don’t already have it)
- Save the script as clean_aspx_to_txt.py next to your justify folder.
python clean_aspx_to_txt.py
That’s it—the folder clean will be built automatically, containing .txt files with all in-line newlines removed except where a <br> explicitly existed.
Let me know if you’d like other tags treated as explicit line breaks, or any further tweaks!