Jan-29-2023, 02:01 PM
Code is not tested. Maybe it works directly, or you have to correct some errors.
Use pathlib.Path.
Don't run this code on Data you don't have a backup of.
Use pathlib.Path.
Don't run this code on Data you don't have a backup of.
import re from pathlib import Path from docx import Document path = Path(r"d:\2022_12_02") if path.exists(): print(path, "exists") else: print(path, "does not exist") raise SystemExit(-1) for file in path.glob("*"): # file is a Path object document = Document() # file.name is the name of the file as str without the Path document.add_heading(file.name, 0) # Path objects do have the read_text, read_bytes # method and also supports # open with context managers # remove all non-XML-compatible characters file_content = re.sub(r"[^\x00-\x7F]+|\x0c", " ", file.read_text()) document.add_paragraph(file_content) # if Document could not handle Path objects, # you must convert the Path object to a str # document.save(str(file)) document.save(file)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!