-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TLDR-451 tutorial new doc type (#331)
* docs added * add code testing * some fixes * some fixes * add tabula and some fixes * add python-djvulibre * delete python-djvulibre and add djvulibre-bin * add poppler-utils * add tesseract * some fixes * flake8 stylefix * fix docs after flake8 * update last part of adding_new_doc_type_tutorial * rewrite dedoc_add_new_doc_type_tutorial * minor fixes * minor fixes * minor fixes * some fixes * add more code examples * some fixes --------- Co-authored-by: Nikita Shevtsov <shevtsov@ispras.ru> Co-authored-by: Nasty <bogatenkova.anastasiya@mail.ru>
- Loading branch information
1 parent
8fe955f
commit db202e7
Showing
15 changed files
with
513 additions
and
137 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -16,7 +16,6 @@ exclude = | |
resources, | ||
dedoc/scripts, | ||
examples, | ||
docs, | ||
venv, | ||
build, | ||
dedoc.egg-info | ||
|
This file contains 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
61 changes: 61 additions & 0 deletions
61
docs/source/_static/code_examples/dedoc_add_new_doc_type_tutorial.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import mimetypes | ||
import os | ||
|
||
from djvu_converter import DjvuConverter | ||
from pdf_reader import PdfReader | ||
|
||
from dedoc import DedocManager | ||
from dedoc.attachments_handler import AttachmentsHandler | ||
from dedoc.converters import FileConverterComposition | ||
from dedoc.metadata_extractors import BaseMetadataExtractor, DocxMetadataExtractor, MetadataExtractorComposition | ||
from dedoc.readers import ReaderComposition | ||
from dedoc.structure_constructors import LinearConstructor, StructureConstructorComposition, TreeConstructor | ||
from dedoc.structure_extractors import DefaultStructureExtractor, StructureExtractorComposition | ||
|
||
|
||
file_dir, file_name = "test_dir", "example_with_table.djvu" | ||
file_path = os.path.join(file_dir, file_name) | ||
|
||
|
||
djvu_converter = DjvuConverter(config=dict()) | ||
pdf_reader = PdfReader() | ||
|
||
name_wo_extension, file_extension = os.path.splitext(file_name) | ||
file_mime = mimetypes.guess_type(file_path)[0] | ||
|
||
djvu_converter.can_convert(file_extension, file_mime) # True | ||
djvu_converter.do_convert(file_dir, name_wo_extension, file_extension) # 'example_with_table.pdf' | ||
|
||
file_dir, file_name = "test_dir", "example_with_attachments_depth_1.pdf" | ||
file_path = os.path.join(file_dir, file_name) | ||
|
||
name_wo_extension, file_extension = os.path.splitext(file_name) | ||
file_mime = mimetypes.guess_type(file_path)[0] | ||
pdf_reader.can_read(file_path, file_mime, file_extension) # True | ||
|
||
pdf_reader.read(file_path, parameters={"with_attachments": "true"}) # <dedoc.data_structures.UnstructuredDocument> | ||
|
||
document = pdf_reader.read(file_path, parameters={"with_attachments": "true"}) | ||
list(vars(document)) # ['tables', 'lines', 'attachments', 'warnings', 'metadata'] | ||
len(document.attachments) # 2 | ||
len(document.lines) # 15 | ||
|
||
"""Adding the implemented handlers to the manager config""" | ||
config = {} | ||
manager_config = dict( | ||
converter=FileConverterComposition(converters=[DjvuConverter(config=config)]), | ||
reader=ReaderComposition(readers=[PdfReader()]), | ||
structure_extractor=StructureExtractorComposition(extractors={DefaultStructureExtractor.document_type: DefaultStructureExtractor()}, default_key="other"), | ||
structure_constructor=StructureConstructorComposition( | ||
constructors={"linear": LinearConstructor(), "tree": TreeConstructor()}, | ||
default_constructor=LinearConstructor() | ||
), | ||
document_metadata_extractor=MetadataExtractorComposition(extractors=[DocxMetadataExtractor(), BaseMetadataExtractor()]), | ||
attachments_handler=AttachmentsHandler(config=config), | ||
) | ||
|
||
manager = DedocManager(config=config, manager_config=manager_config) | ||
result = manager.parse(file_path=file_path, parameters={"with_attachments": "true"}) | ||
|
||
result # <dedoc.data_structures.ParsedDocument> | ||
result.to_dict() # OrderedDict([('version', '0.11.2'), ('warnings', []), ('content', OrderedDict([('structure', OrderedDict([('node_id', '0'), ... |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import os | ||
from typing import Optional | ||
|
||
from dedoc.converters.concrete_converters.abstract_converter import AbstractConverter | ||
|
||
|
||
class DjvuConverter(AbstractConverter): | ||
|
||
def __init__(self, config: dict) -> None: | ||
super().__init__(config=config) | ||
|
||
def can_convert(self, extension: str, mime: str, parameters: Optional[dict] = None) -> bool: | ||
return extension == ".djvu" | ||
|
||
def do_convert(self, tmp_dir: str, filename: str, extension: str) -> str: | ||
os.system(f"ddjvu -format=pdf {tmp_dir}/{filename}{extension} {tmp_dir}/{filename}.pdf") | ||
self._await_for_conversion(filename + ".pdf", tmp_dir) | ||
return filename + ".pdf" |
Oops, something went wrong.