Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from docx import Document
- # Path to the folder containing .docx files
- input_folder = "d:/doc"
- # Path to the folder where .txt files will be saved
- output_folder = "d:/doc"
- # Get a list of all .docx files in the input folder
- files = [f for f in os.listdir(input_folder) if f.endswith(".docx")]
- # Loop through each .docx file and convert it to .txt
- for file in files:
- docx_path = os.path.join(input_folder, file)
- txt_path = os.path.join(output_folder, os.path.splitext(file)[0] + ".txt")
- doc = Document(docx_path)
- content = [p.text for p in doc.paragraphs]
- with open(txt_path, "w", encoding="utf-8") as txt_file:
- txt_file.write("\n".join(content))
- print("Conversion complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement