-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitter.py
More file actions
executable file
·44 lines (33 loc) · 1.92 KB
/
splitter.py
File metadata and controls
executable file
·44 lines (33 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
from docx import Document
def split_file(input_file, output_dir, chunk_size=4000):
# Проверяем, существует ли папка для вывода, если нет, создаём её
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Открываем документ .docx
doc = Document(input_file)
# Извлекаем текст из всех параграфов документа
content = ""
for para in doc.paragraphs:
content += para.text + "\n" # Добавляем перенос строки между параграфами
# Разделяем содержимое файла на куски по chunk_size символов
num_chunks = len(content) // chunk_size + (1 if len(content) % chunk_size > 0 else 0)
# Сохраняем каждый кусок в новый .docx файл
for i in range(num_chunks):
chunk_content = content[i * chunk_size : (i + 1) * chunk_size]
# Создаём новый документ
chunk_doc = Document()
chunk_doc.add_paragraph(chunk_content)
# Путь к новому файлу
output_file = os.path.join(output_dir, f'chunk_{i+1}.docx')
# Сохраняем новый документ .docx
chunk_doc.save(output_file)
print(f"Файл {output_file} создан.")
# Запрос пути к файлу и папке
input_file = input("Введите путь к файлу, который нужно разделить: ")
output_dir = input("Введите путь к папке, куда сохранить разделённые файлы: ")
# Проверим, существует ли файл, иначе выведем ошибку
if not os.path.isfile(input_file):
print("Ошибка: файл не найден.")
else:
split_file(input_file, output_dir)