-
Notifications
You must be signed in to change notification settings - Fork 1
✨ Extract Kapitelnummern from .docx #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| """ | ||
| Contains high level functions to process .docx files | ||
| """ | ||
| import itertools | ||
| import logging | ||
| import re | ||
| from io import BytesIO | ||
| from pathlib import Path | ||
| from typing import Dict, Generator, List, Union | ||
| from typing import Dict, Generator, Iterable, List, Tuple, Union | ||
|
|
||
| import attrs | ||
| from docx import Document # type:ignore[import] | ||
| from docx.oxml import CT_P, CT_Tbl # type:ignore[import] | ||
| from docx.table import Table # type:ignore[import] | ||
|
|
@@ -107,21 +109,72 @@ def get_ebd_docx_tables(docx_file_path: Path, ebd_key: str) -> List[Table]: | |
| return tables | ||
|
|
||
|
|
||
| def get_all_ebd_keys(docx_file_path: Path) -> Dict[str, str]: | ||
| # pylint:disable=too-few-public-methods | ||
| @attrs.define(kw_only=True, frozen=True) | ||
| class EbdChapterInformation: | ||
| """ | ||
| Contains information about where an EBD is located within the document. | ||
| If the heading is e.g. "5.2.1" we denote this as: | ||
| * chapter 5 | ||
| * section 2 | ||
| * subsection 1 | ||
| """ | ||
|
|
||
| chapter: int = attrs.field( | ||
| validator=attrs.validators.and_(attrs.validators.instance_of(int), attrs.validators.ge(1)) | ||
| ) | ||
| section: int = attrs.field( | ||
| validator=attrs.validators.and_(attrs.validators.instance_of(int), attrs.validators.ge(1)) | ||
| ) | ||
| subsection: int = attrs.field( | ||
| validator=attrs.validators.and_(attrs.validators.instance_of(int), attrs.validators.ge(1)) | ||
| ) | ||
|
|
||
| def __str__(self): | ||
| return f"{self.chapter}.{self.section}.{self.subsection}" | ||
|
|
||
|
|
||
| def _enrich_paragraphs_with_sections( | ||
| paragraphs: Iterable[Paragraph], | ||
| ) -> Generator[Tuple[Paragraph, EbdChapterInformation], None, None]: | ||
| """ | ||
| Yield each paragraph + the "Kapitel" in which it is found. | ||
| """ | ||
| chapter_counter = itertools.count(start=1) | ||
| chapter = 1 | ||
| section_counter = itertools.count(start=1) | ||
| section = 1 | ||
| subsection_counter = itertools.count(start=1) | ||
| subsection = 1 | ||
| for paragraph in paragraphs: | ||
| match paragraph.style.style_id: | ||
| case "berschrift1": | ||
| chapter = next(chapter_counter) | ||
| section_counter = itertools.count(start=1) | ||
| subsection_counter = itertools.count(start=1) | ||
| case "berschrift2": | ||
| section = next(section_counter) | ||
| subsection_counter = itertools.count(start=1) | ||
| case "berschrift3": | ||
| subsection = next(subsection_counter) | ||
|
Comment on lines
+151
to
+159
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check the tests: the german "ü" is actually discarded |
||
| yield paragraph, EbdChapterInformation(chapter=chapter, section=section, subsection=subsection) | ||
|
|
||
|
|
||
| def get_all_ebd_keys(docx_file_path: Path) -> Dict[str, Tuple[str, EbdChapterInformation]]: | ||
| """ | ||
| Extract all EBD keys from the given file. | ||
| Returns a dictionary with all EBD keys as keys and the respective EBD titles as values. | ||
| E.g. key: "E_0003", value: "Bestellung der Aggregationsebene RZ prüfen" | ||
| """ | ||
| document = get_document(docx_file_path) | ||
| result: Dict[str, str] = {} | ||
| for paragraph in document.paragraphs: | ||
| result: Dict[str, Tuple[str, EbdChapterInformation]] = {} | ||
| for paragraph, ebd_kapitel in _enrich_paragraphs_with_sections(document.paragraphs): | ||
| match = _ebd_key_with_heading_pattern.match(paragraph.text) | ||
| if match is None: | ||
| continue | ||
| ebd_key = match.groupdict()["key"] | ||
| title = match.groupdict()["title"] | ||
| result[ebd_key] = title | ||
| _logger.debug("Found EBD %s: '%s'", ebd_key, title) | ||
| result[ebd_key] = (title, ebd_kapitel) | ||
| _logger.debug("Found EBD %s: '%s' (%s)", ebd_key, title, ebd_kapitel) | ||
| _logger.info("%i EBD keys have been found", len(result)) | ||
| return result | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| from docx.table import Table # type:ignore[import] | ||
| from ebdtable2graph.models import EbdTable | ||
|
|
||
| from ebddocx2table import TableNotFoundError | ||
| from ebddocx2table import EbdChapterInformation, TableNotFoundError | ||
| from ebddocx2table.docxtableconverter import DocxTableConverter | ||
|
|
||
| from . import get_all_ebd_keys, get_document, get_ebd_docx_tables | ||
|
|
@@ -31,12 +31,30 @@ def test_can_read_document(self, datafiles, filename: str): | |
|
|
||
| @pytest.mark.datafiles("unittests/test_data/ebd20221128.docx") | ||
| @pytest.mark.parametrize( | ||
| "filename,expected_length", | ||
| [pytest.param("ebd20221128.docx", 241)], | ||
| "filename,expected_length,expected_entries", | ||
| [ | ||
| pytest.param( | ||
| "ebd20221128.docx", | ||
| 241, | ||
| [ | ||
| # arbitrary checks ("Stichproben") only | ||
| ("Kündigung Stromliefervertrag prüfen", EbdChapterInformation(chapter=6, section=1, subsection=1)), | ||
| ("MaBiS-ZP Aktivierung prüfen", EbdChapterInformation(chapter=7, section=2, subsection=1)), | ||
| ( | ||
| "Datenstatus nach Eingang einer AAÜZ vergeben", | ||
| EbdChapterInformation(chapter=7, section=61, subsection=2), | ||
| ), | ||
|
Comment on lines
+41
to
+46
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. diese drei einträge einfach mit der docx aus den unittests vergleichen |
||
| ], | ||
| ) | ||
| ], | ||
| ) | ||
| def test_get_ebd_keys(self, datafiles, filename: str, expected_length: int): | ||
| def test_get_ebd_keys( | ||
| self, datafiles, filename: str, expected_length: int, expected_entries: List[Tuple[str, EbdChapterInformation]] | ||
| ): | ||
| actual = get_all_ebd_keys(datafiles, filename) | ||
| assert len(actual) == expected_length # arbitrary, didn't check if these are really _all_ the keys | ||
| for expected_entry in expected_entries: | ||
| assert expected_entry in actual.values() | ||
|
|
||
| @pytest.mark.datafiles("unittests/test_data/ebd20221128.docx") | ||
| @pytest.mark.parametrize( | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Braucht es die Klammern um
ebd_title, ebd_kapitel?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sonst hätte pycharm oder black sie entfernt. ich denke s liegt daran, dass ein item aus dem dctionary ein
Tuple[Key, Value]ist und Value seinerseits ein Tuple[titel, kapitel].