Spaces:
Sleeping
Sleeping
Update split_files_to_excel.py
Browse files- split_files_to_excel.py +93 -91
split_files_to_excel.py
CHANGED
|
@@ -527,101 +527,103 @@ def split_by_keywords(files, key_words,words_limit=1000):
|
|
| 527 |
|
| 528 |
tabLine = []
|
| 529 |
for file in files:
|
| 530 |
-
|
| 531 |
-
file
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
for
|
| 543 |
-
line
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
# Load previous page
|
| 568 |
-
else:
|
| 569 |
-
try:
|
| 570 |
-
selectedPdfPage -= 1
|
| 571 |
-
newLoad_page = file.get_page(selectedPdfPage)
|
| 572 |
-
newText = newLoad_page.extract_text()
|
| 573 |
-
newLines = newText.split("\n")
|
| 574 |
-
linesForSelection = newLines
|
| 575 |
-
lineIndex = len(newLines) - 1
|
| 576 |
-
|
| 577 |
-
except Exception as e:
|
| 578 |
-
print(f"Loading previous PDF page failed")
|
| 579 |
lineBool = False
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
lineBool = False
|
| 603 |
-
|
| 604 |
-
if
|
| 605 |
lineBool = False
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
else:
|
| 609 |
-
try:
|
| 610 |
-
selectedPdfPage += 1
|
| 611 |
-
newLoad_page = file.get_page(selectedPdfPage)
|
| 612 |
-
newText = newLoad_page.extract_text()
|
| 613 |
-
newLines = newText.split("\n")
|
| 614 |
-
linesForSelection = newLines
|
| 615 |
-
lineIndex = 0
|
| 616 |
-
except Exception as e:
|
| 617 |
-
print(f"Loading next PDF page failed")
|
| 618 |
lineBool = False
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
|
| 626 |
for r in tabLine:
|
| 627 |
text_joined = ''.join(r[1])
|
|
|
|
| 527 |
|
| 528 |
tabLine = []
|
| 529 |
for file in files:
|
| 530 |
+
|
| 531 |
+
if file.endswith('pdf'):
|
| 532 |
+
|
| 533 |
+
file = PdfReader(file)
|
| 534 |
+
pdfNumberPages = len(file.pages)
|
| 535 |
+
for pdfPage in range(0, pdfNumberPages):
|
| 536 |
+
|
| 537 |
+
load_page = file.get_page(pdfPage)
|
| 538 |
+
text = load_page.extract_text()
|
| 539 |
+
lines = text.split("\n")
|
| 540 |
+
sizeOfLines = len(lines) - 1
|
| 541 |
+
|
| 542 |
+
for index, line in enumerate(lines):
|
| 543 |
+
print(line)
|
| 544 |
+
for key in key_words:
|
| 545 |
+
line = line.lower()
|
| 546 |
+
|
| 547 |
+
if key in line:
|
| 548 |
+
print("Found keyword")
|
| 549 |
+
|
| 550 |
+
# Init variables for search
|
| 551 |
+
lineBool = True
|
| 552 |
+
lineIndex = index
|
| 553 |
+
previousSelectedLines = []
|
| 554 |
+
stringLength = 0
|
| 555 |
+
linesForSelection = lines
|
| 556 |
+
loadOnce = True
|
| 557 |
+
selectedPdfPage = pdfPage
|
| 558 |
+
|
| 559 |
+
# Loop while extracting text before keyword
|
| 560 |
+
while lineBool:
|
| 561 |
+
print(lineIndex)
|
| 562 |
+
if stringLength > words_limit or lineIndex < 0:
|
| 563 |
+
lineBool = False
|
| 564 |
+
else:
|
| 565 |
+
if lineIndex == 0:
|
| 566 |
+
if pdfPage == 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
lineBool = False
|
| 568 |
+
|
| 569 |
+
# Load previous page
|
| 570 |
+
else:
|
| 571 |
+
try:
|
| 572 |
+
selectedPdfPage -= 1
|
| 573 |
+
newLoad_page = file.get_page(selectedPdfPage)
|
| 574 |
+
newText = newLoad_page.extract_text()
|
| 575 |
+
newLines = newText.split("\n")
|
| 576 |
+
linesForSelection = newLines
|
| 577 |
+
lineIndex = len(newLines) - 1
|
| 578 |
+
|
| 579 |
+
except Exception as e:
|
| 580 |
+
print(f"Loading previous PDF page failed")
|
| 581 |
+
lineBool = False
|
| 582 |
+
|
| 583 |
+
previousSelectedLines.append(linesForSelection[lineIndex])
|
| 584 |
+
stringLength += len(linesForSelection[lineIndex])
|
| 585 |
+
|
| 586 |
+
lineIndex -= 1
|
| 587 |
+
previousSelectedLines = ' '.join(previousSelectedLines[::-1])
|
| 588 |
+
|
| 589 |
+
# Init variables for search
|
| 590 |
+
lineBool = True
|
| 591 |
+
lineIndex = index + 1
|
| 592 |
+
nextSelectedLines = ""
|
| 593 |
+
linesForSelection = lines
|
| 594 |
+
loadOnce = True
|
| 595 |
+
selectedPdfPage = pdfPage
|
| 596 |
+
|
| 597 |
+
# Loop while extracting text after keyword
|
| 598 |
+
while lineBool:
|
| 599 |
+
|
| 600 |
+
if len(nextSelectedLines.split()) > words_limit:
|
| 601 |
lineBool = False
|
| 602 |
+
else:
|
| 603 |
+
if lineIndex > sizeOfLines:
|
| 604 |
lineBool = False
|
| 605 |
+
|
| 606 |
+
if pdfPage == pdfNumberPages - 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 607 |
lineBool = False
|
| 608 |
+
|
| 609 |
+
# Load next page
|
| 610 |
+
else:
|
| 611 |
+
try:
|
| 612 |
+
selectedPdfPage += 1
|
| 613 |
+
newLoad_page = file.get_page(selectedPdfPage)
|
| 614 |
+
newText = newLoad_page.extract_text()
|
| 615 |
+
newLines = newText.split("\n")
|
| 616 |
+
linesForSelection = newLines
|
| 617 |
+
lineIndex = 0
|
| 618 |
+
except Exception as e:
|
| 619 |
+
print(f"Loading next PDF page failed")
|
| 620 |
+
lineBool = False
|
| 621 |
+
else:
|
| 622 |
+
nextSelectedLines += " " + linesForSelection[lineIndex]
|
| 623 |
+
lineIndex += 1
|
| 624 |
+
|
| 625 |
+
selectedText = previousSelectedLines + ' ' + nextSelectedLines
|
| 626 |
+
tabLine.append([pdfPage, selectedText, key])
|
| 627 |
|
| 628 |
for r in tabLine:
|
| 629 |
text_joined = ''.join(r[1])
|