-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParsing.py
More file actions
65 lines (54 loc) · 2.08 KB
/
Copy pathParsing.py
File metadata and controls
65 lines (54 loc) · 2.08 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from docx import Document
from openpyxl import load_workbook
from openpyxl.styles import PatternFill
from openpyxl.styles import Font, Color
from openpyxl.utils import get_column_letter
id = 9
number_of_id_row = int
name_of_column = int
formatting = []
numbering = []
def get_numbering_paragraphs(doc):
numbering_paragraphs = []
for paragraph in doc.paragraphs:
if paragraph._p.pPr is None:
continue
numbering_paragraphs.append(paragraph)
return numbering_paragraphs
def parse_word_file1(file_path):
doc = Document(file_path)
numbering_paragraphs = get_numbering_paragraphs(doc)
for paragraph in numbering_paragraphs:
numbering_text = paragraph.text
# print(numbering_text)
def parse_word_file(word_file, excel_file, sheet_name, start_keyword, end_keyword):
# Загружаем документ Word
doc = Document(word_file)
# Загружаем книгу Excel и выбираем нужный лист
workbook = load_workbook(excel_file)
sheet = workbook[sheet_name]
print(sheet.max_column)
for i in range(1, sheet.max_column):
print(sheet.cell(row=1, column=i).value)
if sheet.cell(row=1, column=i).value == 'показания к применению':
name_of_column = i
print(name_of_column)
for i in range(1, sheet.max_row):
if sheet.cell(row=i, column=1).value == id:
number_of_id_row = i
print(number_of_id_row)
# Ищем нужные абзацы в документе Word
start = False
text = ''
for paragraph in doc.paragraphs:
if start_keyword in paragraph.text:
start = True
elif end_keyword in paragraph.text:
start = False
# Вставляем текст в следующую свободную ячейку Excel
sheet.cell(row=number_of_id_row, column=name_of_column).value = text
text = ''
elif start:
text += paragraph.text
# Сохраняем изменения в файле Excel
workbook.save(excel_file)