Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from tkinter import Tk, filedialog
- import re
- import chardet
- def get_file_path():
- """Displays a file dialog to select a file."""
- root = Tk()
- root.withdraw()
- file_path = filedialog.askopenfilename(
- title="Select a File",
- filetypes=(
- ("Text Files", "*.txt"),
- ("All Files", "*.*")
- )
- )
- return file_path
- def get_max_words():
- """Get maximum number of words per file from user"""
- while True:
- try:
- max_words = int(input("Enter maximum number of words per file: "))
- if max_words > 0:
- return max_words
- print("Please enter a positive number.")
- except ValueError:
- print("Please enter a valid number.")
- def count_words(text):
- """Count words in text"""
- return len(text.split())
- def split_into_sentences(text):
- """Split text into sentences while preserving original formatting"""
- # Split by common sentence endings but preserve them in the result
- sentences = []
- current_sentence = ""
- for char in text:
- current_sentence += char
- # Check for sentence endings
- if char in '.!?' and len(current_sentence.strip()) > 0:
- # Look ahead for quotation marks or other ending characters
- sentences.append(current_sentence)
- current_sentence = ""
- # Add any remaining text as a sentence
- if current_sentence:
- sentences.append(current_sentence)
- return sentences
- def detect_encoding(file_path):
- """Detect the encoding of a file using chardet"""
- with open(file_path, 'rb') as file:
- raw_data = file.read()
- result = chardet.detect(raw_data)
- encoding = result['encoding']
- confidence = result['confidence']
- print(f"Detected encoding: {encoding} with confidence: {confidence:.2%}")
- return encoding
- def split_file(file_path, max_words):
- try:
- # Detect file encoding
- encoding = detect_encoding(file_path)
- if not encoding:
- encoding = 'cp1250' # Try with common Windows encoding for Polish
- print(f"Could not detect encoding, trying with {encoding}")
- # Read file content with detected encoding
- try:
- with open(file_path, 'r', encoding=encoding) as file:
- content = file.read()
- except UnicodeDecodeError:
- # If still fails, try with cp1250 (common for Polish text files)
- encoding = 'cp1250'
- print(f"Failed with detected encoding, trying with {encoding}")
- with open(file_path, 'r', encoding=encoding) as file:
- content = file.read()
- # Get file path info
- directory, filename = os.path.split(file_path)
- base_name, ext = os.path.splitext(filename)
- # Split into sentences
- sentences = split_into_sentences(content)
- current_part = ""
- current_words = 0
- part_number = 1
- for sentence in sentences:
- sentence_words = count_words(sentence)
- # If adding this sentence would exceed max_words and we already have content,
- # save current part and start new one
- if current_words + sentence_words > max_words and current_part:
- # Save current part
- output_file = f"{base_name}_part{part_number}{ext}"
- output_path = os.path.join(directory, output_file)
- with open(output_path, 'w', encoding=encoding) as out_file:
- out_file.write(current_part)
- # Start new part
- current_part = sentence
- current_words = sentence_words
- part_number += 1
- else:
- # Add sentence to current part
- current_part += sentence
- current_words += sentence_words
- # Save last part if anything remains
- if current_part:
- output_file = f"{base_name}_part{part_number}{ext}"
- output_path = os.path.join(directory, output_file)
- with open(output_path, 'w', encoding=encoding) as out_file:
- out_file.write(current_part)
- print(f"File split into {part_number} parts successfully.")
- print(f"Encoding used: {encoding}")
- print("Note: Some parts might slightly exceed the word limit to preserve complete sentences.")
- except Exception as e:
- print(f"An error occurred: {e}")
- if __name__ == "__main__":
- file_path = get_file_path()
- if file_path:
- max_words = get_max_words()
- split_file(file_path, max_words)
- else:
- print("No file selected. Exiting.")
Advertisement
Add Comment
Please, Sign In to add comment