convert-stream 2.2.5__tar.gz → 2.2.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {convert_stream-2.2.5 → convert_stream-2.2.7}/PKG-INFO +5 -5
- {convert_stream-2.2.5 → convert_stream-2.2.7}/README.md +4 -4
- convert_stream-2.2.7/convert_stream/__init__.py +90 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/_version.py +2 -2
- convert_stream-2.2.7/convert_stream/enum_libs/__init__.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/enum_libs/enums.py +1 -1
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/enum_libs/modules.py +11 -11
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/image/img_object.py +2 -6
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/pdf/__init__.py +4 -3
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/pdf/image_to_pdf.py +10 -11
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/pdf/pdf_document.py +110 -69
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/pdf/pdf_page.py +17 -21
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/pdf/pdf_to_image.py +60 -50
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/sheets/load.py +14 -19
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/table_files.py +1 -1
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream.egg-info/PKG-INFO +5 -5
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream.egg-info/SOURCES.txt +2 -1
- {convert_stream-2.2.5 → convert_stream-2.2.7}/pyproject.toml +1 -1
- convert_stream-2.2.5/convert_stream/__init__.py +0 -31
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/__main__.py +0 -0
- /convert_stream-2.2.5/convert_stream/stream.py → /convert_stream-2.2.7/convert_stream/doc_stream.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/image/__init__.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/sheets/__init__.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/sheets/parse.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/sheets/save.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/text/__init__.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/text/date.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/text/string.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream/text/terminal.py +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream.egg-info/dependency_links.txt +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream.egg-info/requires.txt +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/convert_stream.egg-info/top_level.txt +0 -0
- {convert_stream-2.2.5 → convert_stream-2.2.7}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: convert_stream
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.7
|
|
4
4
|
Summary: Manipulação de documentos PDF e Imagens. Converter, Ler, rotacionar exportar etc.
|
|
5
5
|
Author: Bruno Chaves
|
|
6
6
|
Requires-Python: >=3.11
|
|
@@ -24,12 +24,12 @@ pip3 install -r requirements.txt
|
|
|
24
24
|
>>> from file import File, Directory
|
|
25
25
|
>>> from pdflib import PdfStream
|
|
26
26
|
# instâncie o caminho do arquivo PDF e o diretório para exportar os dados.
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
file_pdf = File('path/to/file.pdf')
|
|
28
|
+
output_dir = Directory('path/to/save')
|
|
29
29
|
|
|
30
30
|
pdf_stream = PdfStream()
|
|
31
|
-
pdf_stream.add_file_pdf(
|
|
32
|
-
pdf_stream.to_files_pdf(
|
|
31
|
+
pdf_stream.add_file_pdf(file_pdf)
|
|
32
|
+
pdf_stream.to_files_pdf(output_dir) # isso irá converter cada página em um arquivo PDF
|
|
33
33
|
|
|
34
34
|
# Uso para unir vários arquivos PDF.
|
|
35
35
|
>>> from file import File, Directory
|
|
@@ -6,12 +6,12 @@ pip3 install -r requirements.txt
|
|
|
6
6
|
>>> from file import File, Directory
|
|
7
7
|
>>> from pdflib import PdfStream
|
|
8
8
|
# instâncie o caminho do arquivo PDF e o diretório para exportar os dados.
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
file_pdf = File('path/to/file.pdf')
|
|
10
|
+
output_dir = Directory('path/to/save')
|
|
11
11
|
|
|
12
12
|
pdf_stream = PdfStream()
|
|
13
|
-
pdf_stream.add_file_pdf(
|
|
14
|
-
pdf_stream.to_files_pdf(
|
|
13
|
+
pdf_stream.add_file_pdf(file_pdf)
|
|
14
|
+
pdf_stream.to_files_pdf(output_dir) # isso irá converter cada página em um arquivo PDF
|
|
15
15
|
|
|
16
16
|
# Uso para unir vários arquivos PDF.
|
|
17
17
|
>>> from file import File, Directory
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from io import BytesIO
|
|
4
|
+
from typing import Union
|
|
5
|
+
from soup_files import File, Directory, LibraryDocs, InputFiles
|
|
6
|
+
|
|
7
|
+
from ._version import (
|
|
8
|
+
__module_name__, __author__, __license__, __modify_date__, __version__
|
|
9
|
+
)
|
|
10
|
+
from convert_stream.enum_libs.enums import *
|
|
11
|
+
from convert_stream.enum_libs.modules import (
|
|
12
|
+
LibPDF, LibDate, LibImage, LibPdfToImage, LibImageToPdf,
|
|
13
|
+
DEFAULT_LIB_PDF, DEFAULT_LIB_IMAGE, DEFAULT_LIB_PDF_TO_IMG,
|
|
14
|
+
DEFAULT_LIB_IMAGE_TO_PDF, ModPdfToImage, ModPagePdf, ModuleImage,
|
|
15
|
+
ModImageToPdf, MOD_FITZ, MOD_PYPDF, MOD_CANVAS, MOD_IMG_PIL, MOD_IMG_OPENCV,
|
|
16
|
+
)
|
|
17
|
+
from .text.terminal import print_title, print_line
|
|
18
|
+
from .text.string import (
|
|
19
|
+
FindText, ArrayString, MapText
|
|
20
|
+
)
|
|
21
|
+
from .text.date import ConvertStringDate
|
|
22
|
+
from convert_stream.image.img_object import (
|
|
23
|
+
ImageObject, Image, MatLike, get_hash_from_bytes, CollectionImages,
|
|
24
|
+
)
|
|
25
|
+
from convert_stream.pdf import (
|
|
26
|
+
PageDocumentPdf, DocumentPdf, ConvertImageToPdf, ConvertPdfToImages,
|
|
27
|
+
ModImageToPdf, ModDocPdf, CollectionPagePdf, DEFAULT_LIB_PDF,
|
|
28
|
+
DEFAULT_LIB_IMAGE_TO_PDF, DEFAULT_LIB_PDF_TO_IMG
|
|
29
|
+
)
|
|
30
|
+
from .doc_stream import PdfStream, SplitPdf
|
|
31
|
+
from .sheets import save_data, ReadFileSheet
|
|
32
|
+
from .table_files import (
|
|
33
|
+
get_void_df, create_df_from_file_pdf, create_map_from_values,
|
|
34
|
+
FileToTable, ColumnsTable, PdfFinder, SearchableTextPdf
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def __create_stream() -> PdfStream:
|
|
39
|
+
st = PdfStream()
|
|
40
|
+
st.clear()
|
|
41
|
+
return st
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def read_pdf(file: Union[str, File, bytes, BytesIO]) -> PdfStream:
|
|
45
|
+
_pdf_stream = __create_stream()
|
|
46
|
+
|
|
47
|
+
if isinstance(file, File):
|
|
48
|
+
_pdf_stream.add_file_pdf(file)
|
|
49
|
+
elif isinstance(file, str):
|
|
50
|
+
_pdf_stream.add_file_pdf(File(file))
|
|
51
|
+
elif isinstance(file, bytes):
|
|
52
|
+
_pdf_stream.add_document(DocumentPdf(BytesIO(file)))
|
|
53
|
+
elif isinstance(file, BytesIO):
|
|
54
|
+
_pdf_stream.add_document(DocumentPdf(file))
|
|
55
|
+
else:
|
|
56
|
+
raise ValueError()
|
|
57
|
+
return _pdf_stream
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def read_files_pdf(files: Union[list[str], list[File]]) -> PdfStream:
|
|
61
|
+
_pdf_stream = __create_stream()
|
|
62
|
+
if len(files) == 0:
|
|
63
|
+
return _pdf_stream
|
|
64
|
+
|
|
65
|
+
if isinstance(files[0], str):
|
|
66
|
+
for f in files:
|
|
67
|
+
_pdf_stream.add_file_pdf(File(f))
|
|
68
|
+
elif isinstance(files[0], File):
|
|
69
|
+
_pdf_stream.add_files_pdf(files)
|
|
70
|
+
else:
|
|
71
|
+
raise ValueError()
|
|
72
|
+
return _pdf_stream
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def read_image(image: Union[str, File, bytes, BytesIO]) -> PdfStream:
|
|
76
|
+
_pdf_stream = __create_stream()
|
|
77
|
+
if isinstance(image, str):
|
|
78
|
+
_pdf_stream.add_file_image(File(image))
|
|
79
|
+
elif isinstance(image, File):
|
|
80
|
+
_pdf_stream.add_image(ImageObject.create_from_file(image))
|
|
81
|
+
elif isinstance(image, bytes):
|
|
82
|
+
_pdf_stream.add_image(ImageObject.create_from_bytes(image))
|
|
83
|
+
elif isinstance(image, BytesIO):
|
|
84
|
+
image.seek(0)
|
|
85
|
+
_pdf_stream.add_image(ImageObject.create_from_bytes(image.getvalue()))
|
|
86
|
+
else:
|
|
87
|
+
raise ValueError()
|
|
88
|
+
return _pdf_stream
|
|
89
|
+
|
|
90
|
+
|
|
File without changes
|
|
@@ -54,9 +54,9 @@ except ImportError:
|
|
|
54
54
|
import pymupdf as fitz
|
|
55
55
|
MOD_FITZ = True
|
|
56
56
|
except ImportError:
|
|
57
|
-
fitz =
|
|
58
|
-
fitz.Page =
|
|
59
|
-
fitz.TextPage =
|
|
57
|
+
fitz = object
|
|
58
|
+
fitz.Page = object
|
|
59
|
+
fitz.TextPage = object
|
|
60
60
|
|
|
61
61
|
try:
|
|
62
62
|
from pypdf import PdfWriter, PdfReader, PageObject
|
|
@@ -68,20 +68,20 @@ except ImportError:
|
|
|
68
68
|
|
|
69
69
|
|
|
70
70
|
if MOD_FITZ and MOD_PYPDF:
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
ModPagePdf = Union[PageObject, fitz.Page]
|
|
72
|
+
ModDocPdf = Union[fitz.Document, PdfWriter]
|
|
73
73
|
DEFAULT_LIB_PDF = LibPDF.FITZ
|
|
74
74
|
elif MOD_FITZ:
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
ModPagePdf = Union[fitz.Page]
|
|
76
|
+
ModDocPdf = Union[fitz.Document]
|
|
77
77
|
DEFAULT_LIB_PDF = LibPDF.FITZ
|
|
78
78
|
elif MOD_PYPDF:
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
ModPagePdf = Union[PageObject]
|
|
80
|
+
ModDocPdf = Union[PdfWriter]
|
|
81
81
|
DEFAULT_LIB_PDF = LibPDF.PYPDF
|
|
82
82
|
else:
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
ModPagePdf = None # fallback para runtime
|
|
84
|
+
ModDocPdf = None
|
|
85
85
|
DEFAULT_LIB_PDF = LibPDF.NOT_IMPLEMENTED
|
|
86
86
|
|
|
87
87
|
#=================================================================#
|
|
@@ -12,8 +12,8 @@ import numpy as np
|
|
|
12
12
|
from scipy.ndimage import uniform_filter
|
|
13
13
|
from convert_stream.enum_libs.enums import LibImage, RotationAngle
|
|
14
14
|
from convert_stream.enum_libs.modules import (
|
|
15
|
-
|
|
16
|
-
cv2, MatLike, Image,
|
|
15
|
+
MOD_IMG_PIL, ModuleImage, DEFAULT_LIB_IMAGE,
|
|
16
|
+
cv2, MatLike, Image, ImageFilter
|
|
17
17
|
)
|
|
18
18
|
|
|
19
19
|
|
|
@@ -578,7 +578,3 @@ class CollectionImages(object):
|
|
|
578
578
|
def set_pbar(self, p: ProgressBarAdapter) -> None:
|
|
579
579
|
self.pbar = p
|
|
580
580
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
from convert_stream.enum_libs.enums import LibPDF
|
|
4
4
|
from convert_stream.enum_libs.modules import (
|
|
5
5
|
DEFAULT_LIB_PDF, DEFAULT_LIB_PDF_TO_IMG, DEFAULT_LIB_IMAGE_TO_PDF,
|
|
6
|
-
|
|
6
|
+
ModPagePdf, MOD_FITZ, MOD_PYPDF, ModPdfToImage, ModImageToPdf,
|
|
7
|
+
PdfWriter, PdfReader
|
|
7
8
|
)
|
|
8
|
-
from .pdf_page import PageDocumentPdf, fitz
|
|
9
|
+
from .pdf_page import PageDocumentPdf, fitz
|
|
9
10
|
from .pdf_document import (
|
|
10
|
-
DocumentPdf, DEFAULT_LIB_PDF,
|
|
11
|
+
DocumentPdf, DEFAULT_LIB_PDF, ModDocPdf, CollectionPagePdf
|
|
11
12
|
)
|
|
12
13
|
from .pdf_to_image import ConvertPdfToImages
|
|
13
14
|
from .image_to_pdf import ConvertImageToPdf
|
|
@@ -9,7 +9,7 @@ from soup_files import File, Directory, ProgressBarAdapter
|
|
|
9
9
|
from abc import ABC, abstractmethod
|
|
10
10
|
from convert_stream.enum_libs.enums import LibImageToPdf
|
|
11
11
|
from convert_stream.enum_libs.modules import (
|
|
12
|
-
|
|
12
|
+
MOD_CANVAS, DEFAULT_LIB_IMAGE_TO_PDF, canvas, letter, ImageReader
|
|
13
13
|
)
|
|
14
14
|
from convert_stream.pdf import DocumentPdf
|
|
15
15
|
from convert_stream.pdf.pdf_page import fitz
|
|
@@ -91,7 +91,7 @@ class ImplementImageToPdfCanvas(ABCConvertImageToPdf):
|
|
|
91
91
|
c.drawImage(img_reader, 0, 0, width=first_img_width, height=first_img_height)
|
|
92
92
|
|
|
93
93
|
# Processa as imagens restantes
|
|
94
|
-
for num, img_obj in enumerate(self.collection_images[1:], 1):
|
|
94
|
+
for num, img_obj in enumerate(self.collection_images.images[1:], 1):
|
|
95
95
|
|
|
96
96
|
self.pbar.update(
|
|
97
97
|
((num + 1) / max_num) * 100,
|
|
@@ -174,7 +174,7 @@ class ImplementImageToPdfPil(ABCConvertImageToPdf):
|
|
|
174
174
|
|
|
175
175
|
# Redimensiona a imagem
|
|
176
176
|
resized_image = original_pil_image.resize(
|
|
177
|
-
|
|
177
|
+
(scaled_width, scaled_height), Image.Resampling.LANCZOS
|
|
178
178
|
)
|
|
179
179
|
|
|
180
180
|
# Cria uma nova imagem de fundo branca com o tamanho A4
|
|
@@ -237,8 +237,8 @@ class ImplementImageToPdfFitz(ABCConvertImageToPdf):
|
|
|
237
237
|
|
|
238
238
|
for num, img_obj in enumerate(self.collection_images.images):
|
|
239
239
|
self.pbar.update(
|
|
240
|
-
((num+1)/max_num) * 100,
|
|
241
|
-
f'Convertendo: [{num+1} de {max_num}]'
|
|
240
|
+
((num + 1) / max_num) * 100,
|
|
241
|
+
f'Convertendo: [{num + 1} de {max_num}]'
|
|
242
242
|
)
|
|
243
243
|
if land_scape:
|
|
244
244
|
img_obj.set_landscape()
|
|
@@ -294,11 +294,11 @@ class ConvertImageToPdf(object):
|
|
|
294
294
|
"""
|
|
295
295
|
|
|
296
296
|
def __init__(
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
297
|
+
self,
|
|
298
|
+
collection_images: CollectionImages = CollectionImages(),
|
|
299
|
+
*,
|
|
300
|
+
lib_img_to_pdf: LibImageToPdf = DEFAULT_LIB_IMAGE_TO_PDF
|
|
301
|
+
):
|
|
302
302
|
if lib_img_to_pdf == LibImageToPdf.IMAGE_TO_PDF_CANVAS:
|
|
303
303
|
self.convert: ABCConvertImageToPdf = ImplementImageToPdfCanvas(collection_images)
|
|
304
304
|
elif lib_img_to_pdf == LibImageToPdf.IMAGE_TO_PDF_PIL:
|
|
@@ -347,4 +347,3 @@ class ConvertImageToPdf(object):
|
|
|
347
347
|
|
|
348
348
|
def set_land_scape(self):
|
|
349
349
|
self.convert.collection_images.set_land_scape()
|
|
350
|
-
|
|
@@ -6,16 +6,16 @@
|
|
|
6
6
|
from __future__ import annotations
|
|
7
7
|
from abc import ABC, abstractmethod
|
|
8
8
|
from io import BytesIO
|
|
9
|
-
|
|
9
|
+
from typing import Union
|
|
10
10
|
import pandas as pd
|
|
11
11
|
from soup_files import File, Directory, LibraryDocs, InputFiles, ProgressBarAdapter, JsonConvert
|
|
12
12
|
from convert_stream.enum_libs.enums import LibPDF, ColumnsTable
|
|
13
13
|
from convert_stream.enum_libs.modules import (
|
|
14
|
-
DEFAULT_LIB_PDF,
|
|
14
|
+
DEFAULT_LIB_PDF, ModDocPdf, PdfWriter, PdfReader, PageObject, fitz,
|
|
15
15
|
)
|
|
16
16
|
from convert_stream.image.img_object import get_hash_from_bytes
|
|
17
17
|
from convert_stream.pdf.pdf_page import PageDocumentPdf
|
|
18
|
-
from convert_stream.text import
|
|
18
|
+
from convert_stream.text import print_line, show_warning, MapText, FindText
|
|
19
19
|
from convert_stream.sheets.save import save_data
|
|
20
20
|
|
|
21
21
|
|
|
@@ -23,7 +23,7 @@ class SearchableTextPdf(object):
|
|
|
23
23
|
"""
|
|
24
24
|
Esta classe serve para armazenar textos filtrados em documentos PDF
|
|
25
25
|
guardando o texto buscado e outros elementos como a página em que o texto foi
|
|
26
|
-
encontrado, linha e o nome do arquivo.
|
|
26
|
+
encontrado, linha e o nome do respectivo arquivo.
|
|
27
27
|
"""
|
|
28
28
|
def __init__(self, silent: bool = False):
|
|
29
29
|
self.silent = silent
|
|
@@ -81,6 +81,7 @@ class SearchableTextPdf(object):
|
|
|
81
81
|
|
|
82
82
|
def add_line(self, text, *, num_page: str, num_line: str, file: str) -> None:
|
|
83
83
|
if not self.silent:
|
|
84
|
+
print_line()
|
|
84
85
|
print(f'{file} => {text}')
|
|
85
86
|
self.elements[ColumnsTable.NUM_PAGE.value].append(num_page)
|
|
86
87
|
self.elements[ColumnsTable.NUM_LINHA.value].append(num_line)
|
|
@@ -92,6 +93,10 @@ class SearchableTextPdf(object):
|
|
|
92
93
|
self.elements[_k].clear()
|
|
93
94
|
|
|
94
95
|
def to_string(self) -> str:
|
|
96
|
+
"""
|
|
97
|
+
Retorna o texto da coluna TEXT em formato de string
|
|
98
|
+
ou 'nas' em caso de erro nas = Not a String
|
|
99
|
+
"""
|
|
95
100
|
try:
|
|
96
101
|
return ' '.join(self.elements[ColumnsTable.TEXT.value])
|
|
97
102
|
except Exception as e:
|
|
@@ -102,34 +107,36 @@ class SearchableTextPdf(object):
|
|
|
102
107
|
return pd.DataFrame.from_dict(self.elements)
|
|
103
108
|
|
|
104
109
|
def to_file_json(self, file: File):
|
|
110
|
+
"""Exporta os dados da busca para um arquivo .JSON"""
|
|
105
111
|
dt = JsonConvert.from_dict(self.elements).to_json_data()
|
|
106
112
|
dt.to_file(file)
|
|
107
113
|
|
|
108
114
|
def to_file_excel(self, file: File):
|
|
115
|
+
"""Exporta os dados da busca para um arquivo .XLSX"""
|
|
109
116
|
save_data(self.to_data_frame(), file=file)
|
|
110
117
|
|
|
111
118
|
|
|
112
119
|
class ABCDocument(ABC):
|
|
113
120
|
|
|
114
|
-
def __init__(self, mod_doc_pdf:
|
|
115
|
-
self.mod_doc_pdf:
|
|
121
|
+
def __init__(self, mod_doc_pdf: ModDocPdf, name: str):
|
|
122
|
+
self.mod_doc_pdf: ModDocPdf = mod_doc_pdf
|
|
116
123
|
self.lib_pdf: LibPDF = DEFAULT_LIB_PDF
|
|
117
124
|
self.name: str = name
|
|
118
125
|
|
|
119
126
|
@abstractmethod
|
|
120
|
-
def
|
|
127
|
+
def lenght(self) -> int:
|
|
121
128
|
pass
|
|
122
129
|
|
|
123
130
|
@abstractmethod
|
|
124
|
-
def
|
|
131
|
+
def get_first_page(self) -> PageDocumentPdf:
|
|
125
132
|
pass
|
|
126
133
|
|
|
127
134
|
@abstractmethod
|
|
128
|
-
def
|
|
135
|
+
def get_last_page(self) -> PageDocumentPdf:
|
|
129
136
|
pass
|
|
130
137
|
|
|
131
138
|
@abstractmethod
|
|
132
|
-
def
|
|
139
|
+
def get_page(self, idx: int) -> PageDocumentPdf | None:
|
|
133
140
|
pass
|
|
134
141
|
|
|
135
142
|
@abstractmethod
|
|
@@ -163,11 +170,14 @@ class ABCDocument(ABC):
|
|
|
163
170
|
|
|
164
171
|
class ImplementDocumentFitz(ABCDocument):
|
|
165
172
|
|
|
166
|
-
def __init__(self, mod_doc_pdf:
|
|
173
|
+
def __init__(self, mod_doc_pdf: ModDocPdf, name: str):
|
|
167
174
|
super().__init__(mod_doc_pdf, name)
|
|
168
|
-
self.mod_doc_pdf:
|
|
175
|
+
self.mod_doc_pdf: ModDocPdf = mod_doc_pdf
|
|
169
176
|
self.lib_pdf: LibPDF = LibPDF.FITZ
|
|
170
177
|
|
|
178
|
+
def lenght(self) -> int:
|
|
179
|
+
return self.mod_doc_pdf.page_count
|
|
180
|
+
|
|
171
181
|
def get_first_page(self) -> PageDocumentPdf:
|
|
172
182
|
return self.get_page(0)
|
|
173
183
|
|
|
@@ -178,14 +188,11 @@ class ImplementDocumentFitz(ABCDocument):
|
|
|
178
188
|
def get_page(self, idx: int) -> PageDocumentPdf | None:
|
|
179
189
|
_page_pdf = None
|
|
180
190
|
if 0 <= idx < self.mod_doc_pdf.page_count:
|
|
181
|
-
pg = self.mod_doc_pdf.load_page(idx) # retorna fitz.Page
|
|
191
|
+
pg: fitz.Page = self.mod_doc_pdf.load_page(idx) # retorna fitz.Page
|
|
182
192
|
if pg is not None:
|
|
183
|
-
_page_pdf = PageDocumentPdf.create_from_page_fitz(pg)
|
|
193
|
+
_page_pdf = PageDocumentPdf.create_from_page_fitz(pg, pg.number + 1)
|
|
184
194
|
return _page_pdf
|
|
185
195
|
|
|
186
|
-
def get_number_pages(self) -> int:
|
|
187
|
-
return self.mod_doc_pdf.page_count
|
|
188
|
-
|
|
189
196
|
def add_page(self, page: PageDocumentPdf):
|
|
190
197
|
# Assume que page.mod_page é fitz.Page
|
|
191
198
|
self.mod_doc_pdf.insert_pdf(
|
|
@@ -212,15 +219,15 @@ class ImplementDocumentFitz(ABCDocument):
|
|
|
212
219
|
|
|
213
220
|
def to_pages(self) -> list[PageDocumentPdf]:
|
|
214
221
|
pages = []
|
|
215
|
-
for pg in self.mod_doc_pdf:
|
|
216
|
-
page_pdf = PageDocumentPdf.create_from_page_fitz(pg)
|
|
222
|
+
for num, pg in enumerate(self.mod_doc_pdf):
|
|
223
|
+
page_pdf = PageDocumentPdf.create_from_page_fitz(pg, num + 1)
|
|
217
224
|
pages.append(page_pdf)
|
|
218
225
|
return pages
|
|
219
226
|
|
|
220
227
|
@classmethod
|
|
221
228
|
def create_from_bytes(cls, bt: BytesIO) -> ImplementDocumentFitz:
|
|
222
229
|
# Cria documento a partir de BytesIO
|
|
223
|
-
doc:
|
|
230
|
+
doc: ModDocPdf = fitz.open(stream=bt.getvalue(), filetype="pdf")
|
|
224
231
|
name = get_hash_from_bytes(bt)
|
|
225
232
|
return cls(doc, name)
|
|
226
233
|
|
|
@@ -254,6 +261,9 @@ class ImplementDocumentPyPdf(ABCDocument):
|
|
|
254
261
|
self.mod_doc_pdf: PdfWriter = mod_doc_pdf # ou PdfReader
|
|
255
262
|
self.lib_pdf: LibPDF = LibPDF.PYPDF
|
|
256
263
|
|
|
264
|
+
def lenght(self) -> int:
|
|
265
|
+
return len(self.mod_doc_pdf.pages)
|
|
266
|
+
|
|
257
267
|
def get_first_page(self) -> PageDocumentPdf:
|
|
258
268
|
"""
|
|
259
269
|
Retorna a primeira página do documento.
|
|
@@ -281,9 +291,6 @@ class ImplementDocumentPyPdf(ABCDocument):
|
|
|
281
291
|
return PageDocumentPdf.create_from_page_pypdf(pg, idx + 1)
|
|
282
292
|
return None
|
|
283
293
|
|
|
284
|
-
def get_number_pages(self) -> int:
|
|
285
|
-
return len(self.mod_doc_pdf.pages)
|
|
286
|
-
|
|
287
294
|
def add_page(self, page: PageDocumentPdf):
|
|
288
295
|
self.mod_doc_pdf.add_page(page.implement_page_pdf.mod_page)
|
|
289
296
|
|
|
@@ -304,7 +311,7 @@ class ImplementDocumentPyPdf(ABCDocument):
|
|
|
304
311
|
def to_pages(self) -> list[PageDocumentPdf]:
|
|
305
312
|
pages_pdf: list[PageDocumentPdf] = []
|
|
306
313
|
for num, page in enumerate(self.mod_doc_pdf.pages):
|
|
307
|
-
pg = PageDocumentPdf.create_from_page_pypdf(page)
|
|
314
|
+
pg = PageDocumentPdf.create_from_page_pypdf(page, num + 1)
|
|
308
315
|
pages_pdf.append(pg)
|
|
309
316
|
return pages_pdf
|
|
310
317
|
|
|
@@ -339,7 +346,6 @@ class ImplementDocumentPyPdf(ABCDocument):
|
|
|
339
346
|
for page_obj in pages:
|
|
340
347
|
# Obtém o objeto de página da implementação da lib
|
|
341
348
|
pdf_writer.add_page(page_obj.implement_page_pdf.mod_page)
|
|
342
|
-
|
|
343
349
|
output_bytes = BytesIO()
|
|
344
350
|
pdf_writer.write(output_bytes)
|
|
345
351
|
pdf_bytes = output_bytes
|
|
@@ -347,12 +353,62 @@ class ImplementDocumentPyPdf(ABCDocument):
|
|
|
347
353
|
|
|
348
354
|
|
|
349
355
|
class DocumentPdf(object):
|
|
350
|
-
def __init__(
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
356
|
+
def __init__(
|
|
357
|
+
self,
|
|
358
|
+
document: Union[str, File, PdfWriter, fitz.Document, BytesIO],
|
|
359
|
+
*,
|
|
360
|
+
lib_pdf: LibPDF = LibPDF.FITZ
|
|
361
|
+
):
|
|
362
|
+
self.doc_pdf = None
|
|
363
|
+
self.lib_pdf: LibPDF = lib_pdf
|
|
364
|
+
if isinstance(document, str):
|
|
365
|
+
if lib_pdf == LibPDF.PYPDF:
|
|
366
|
+
self.doc_pdf = ImplementDocumentPyPdf.create_from_file(File(document))
|
|
367
|
+
elif lib_pdf == LibPDF.FITZ:
|
|
368
|
+
self.doc_pdf = ImplementDocumentFitz.create_from_file(File(document))
|
|
369
|
+
elif isinstance(document, File):
|
|
370
|
+
if lib_pdf == LibPDF.PYPDF:
|
|
371
|
+
self.doc_pdf = ImplementDocumentPyPdf.create_from_file(document)
|
|
372
|
+
elif lib_pdf == LibPDF.FITZ:
|
|
373
|
+
self.doc_pdf = ImplementDocumentFitz.create_from_file(document)
|
|
374
|
+
elif isinstance(document, PdfWriter):
|
|
375
|
+
_tmp_bytes = BytesIO()
|
|
376
|
+
document.write(_tmp_bytes)
|
|
377
|
+
_tmp_bytes.seek(0)
|
|
378
|
+
if lib_pdf == LibPDF.PYPDF:
|
|
379
|
+
name = get_hash_from_bytes(_tmp_bytes)
|
|
380
|
+
self.doc_pdf = ImplementDocumentPyPdf(document, name)
|
|
381
|
+
del _tmp_bytes
|
|
382
|
+
elif lib_pdf == LibPDF.FITZ:
|
|
383
|
+
self.doc_pdf = ImplementDocumentFitz.create_from_bytes(_tmp_bytes)
|
|
384
|
+
elif isinstance(document, fitz.Document):
|
|
385
|
+
_tmp_bytes = BytesIO(document.tobytes())
|
|
386
|
+
_tmp_bytes.seek(0)
|
|
387
|
+
if lib_pdf == LibPDF.PYPDF:
|
|
388
|
+
self.doc_pdf = ImplementDocumentPyPdf.create_from_bytes(_tmp_bytes)
|
|
389
|
+
elif lib_pdf == LibPDF.FITZ:
|
|
390
|
+
name = get_hash_from_bytes(_tmp_bytes)
|
|
391
|
+
self.doc_pdf = ImplementDocumentFitz(document, name)
|
|
392
|
+
del _tmp_bytes
|
|
393
|
+
elif isinstance(document, BytesIO):
|
|
394
|
+
if lib_pdf == LibPDF.PYPDF:
|
|
395
|
+
self.doc_pdf = ImplementDocumentPyPdf.create_from_bytes(document)
|
|
396
|
+
elif lib_pdf == LibPDF.FITZ:
|
|
397
|
+
self.doc_pdf = ImplementDocumentFitz.create_from_bytes(document)
|
|
398
|
+
elif isinstance(document, ImplementDocumentFitz):
|
|
399
|
+
self.doc_pdf = document
|
|
400
|
+
elif isinstance(document, ImplementDocumentPyPdf):
|
|
401
|
+
self.doc_pdf = document
|
|
402
|
+
|
|
403
|
+
if self.doc_pdf is None:
|
|
404
|
+
raise ValueError(f'documento inválido, use: str, File, PdfWriter, fitz.Document ou BytesIO não => {type(document)}')
|
|
405
|
+
self.name: str = self.doc_pdf.name
|
|
354
406
|
|
|
355
|
-
|
|
407
|
+
@property
|
|
408
|
+
def lenght(self) -> int:
|
|
409
|
+
return self.doc_pdf.lenght()
|
|
410
|
+
|
|
411
|
+
def get_real_document(self) -> fitz.Document | PdfWriter:
|
|
356
412
|
return self.doc_pdf.mod_doc_pdf
|
|
357
413
|
|
|
358
414
|
def get_first_page(self) -> PageDocumentPdf:
|
|
@@ -375,9 +431,6 @@ class DocumentPdf(object):
|
|
|
375
431
|
"""
|
|
376
432
|
return self.doc_pdf.get_page(idx)
|
|
377
433
|
|
|
378
|
-
def get_number_pages(self) -> int:
|
|
379
|
-
return self.doc_pdf.get_number_pages()
|
|
380
|
-
|
|
381
434
|
def add_page(self, page: PageDocumentPdf):
|
|
382
435
|
self.doc_pdf.add_page(page)
|
|
383
436
|
|
|
@@ -521,25 +574,11 @@ class DocumentPdf(object):
|
|
|
521
574
|
|
|
522
575
|
@classmethod
|
|
523
576
|
def create_from_bytes(cls, bt: BytesIO, *, lib_pdf: LibPDF = LibPDF.FITZ) -> DocumentPdf:
|
|
524
|
-
|
|
525
|
-
mod_pdf = ImplementDocumentFitz.create_from_bytes(bt)
|
|
526
|
-
return cls(mod_pdf)
|
|
527
|
-
elif lib_pdf == LibPDF.PYPDF:
|
|
528
|
-
mod_pdf = ImplementDocumentPyPdf.create_from_bytes(bt)
|
|
529
|
-
return cls(mod_pdf)
|
|
530
|
-
else:
|
|
531
|
-
raise NotImplementedError(f'Módulo PDF não implementado: {lib_pdf}')
|
|
577
|
+
return cls(bt, lib_pdf=lib_pdf)
|
|
532
578
|
|
|
533
579
|
@classmethod
|
|
534
580
|
def create_from_file(cls, file: File, *, lib_pdf: LibPDF = DEFAULT_LIB_PDF) -> DocumentPdf:
|
|
535
|
-
|
|
536
|
-
mod_pdf = ImplementDocumentFitz.create_from_file(file)
|
|
537
|
-
return cls(mod_pdf)
|
|
538
|
-
elif lib_pdf == LibPDF.PYPDF:
|
|
539
|
-
mod_pdf = ImplementDocumentPyPdf.create_from_file(file)
|
|
540
|
-
return cls(mod_pdf)
|
|
541
|
-
else:
|
|
542
|
-
raise NotImplementedError(f'Módulo PDF não implementado: {lib_pdf}')
|
|
581
|
+
return cls(file, lib_pdf=lib_pdf)
|
|
543
582
|
|
|
544
583
|
@classmethod
|
|
545
584
|
def create_from_pages(
|
|
@@ -549,10 +588,10 @@ class DocumentPdf(object):
|
|
|
549
588
|
) -> DocumentPdf:
|
|
550
589
|
if lib_pdf == LibPDF.FITZ:
|
|
551
590
|
mod_pdf = ImplementDocumentFitz.create_from_pages(pages)
|
|
552
|
-
return cls(mod_pdf)
|
|
591
|
+
return cls(mod_pdf, lib_pdf=lib_pdf)
|
|
553
592
|
elif lib_pdf == LibPDF.PYPDF:
|
|
554
593
|
mod_pdf = ImplementDocumentPyPdf.create_from_pages(pages)
|
|
555
|
-
return cls(mod_pdf)
|
|
594
|
+
return cls(mod_pdf, lib_pdf=lib_pdf)
|
|
556
595
|
else:
|
|
557
596
|
raise NotImplementedError(f'Módulo PDF não implementado: {lib_pdf}')
|
|
558
597
|
|
|
@@ -561,8 +600,6 @@ class CollectionPagePdf(object):
|
|
|
561
600
|
|
|
562
601
|
def __init__(self, pages: list[PageDocumentPdf] = list()) -> None:
|
|
563
602
|
self.pages: list[PageDocumentPdf] = pages
|
|
564
|
-
self.__count: int = 0
|
|
565
|
-
self.__max_num_pages: int = len(self.pages)
|
|
566
603
|
self.pbar: ProgressBarAdapter = ProgressBarAdapter()
|
|
567
604
|
|
|
568
605
|
@property
|
|
@@ -581,28 +618,25 @@ class CollectionPagePdf(object):
|
|
|
581
618
|
|
|
582
619
|
def clear(self):
|
|
583
620
|
self.pages.clear()
|
|
584
|
-
self.__count = 0
|
|
585
|
-
self.__max_num_pages = 0
|
|
586
621
|
|
|
587
622
|
def set_pbar(self, p: ProgressBarAdapter):
|
|
588
623
|
self.pbar = p
|
|
589
624
|
|
|
590
625
|
def add_page(self, page: PageDocumentPdf) -> None:
|
|
591
|
-
self.
|
|
592
|
-
self.pbar.update_text(f'Adicionando página PDF {
|
|
593
|
-
self.__max_num_pages += 1
|
|
594
|
-
page.number_page = self.__max_num_pages
|
|
626
|
+
page.number_page = len(self.pages) + 1
|
|
627
|
+
self.pbar.update_text(f'Adicionando página PDF {page.number_page}')
|
|
595
628
|
self.pages.append(page)
|
|
596
629
|
|
|
597
630
|
def add_pages(self, pages: list[PageDocumentPdf]) -> None:
|
|
631
|
+
_counter = len(pages)
|
|
598
632
|
for n, pg in enumerate(pages):
|
|
599
|
-
|
|
600
|
-
pg.number_page =
|
|
633
|
+
_counter += 1
|
|
634
|
+
pg.number_page = _counter
|
|
601
635
|
self.pages.append(pg)
|
|
602
636
|
|
|
603
637
|
def add_file_pdf(self, file: File, *, lib_pdf: LibPDF = DEFAULT_LIB_PDF) -> None:
|
|
604
638
|
self.pbar.update_text(f'Adicionando arquivo PDF {file.basename()}')
|
|
605
|
-
doc_pdf = DocumentPdf
|
|
639
|
+
doc_pdf = DocumentPdf(file, lib_pdf=lib_pdf)
|
|
606
640
|
self.add_pages(doc_pdf.to_pages())
|
|
607
641
|
|
|
608
642
|
def add_files_pdf(self, files: list[File], lib_pdf: LibPDF = DEFAULT_LIB_PDF) -> None:
|
|
@@ -612,14 +646,9 @@ class CollectionPagePdf(object):
|
|
|
612
646
|
((num + 1) / max_num) * 100,
|
|
613
647
|
f'Adicionando arquivo: [{num+1} de {max_num}] {f.basename()}'
|
|
614
648
|
)
|
|
615
|
-
_doc_pdf = DocumentPdf
|
|
616
|
-
|
|
617
|
-
for _page in _pages_pdf:
|
|
618
|
-
self.__max_num_pages += 1
|
|
619
|
-
_page.number_page = self.__max_num_pages
|
|
620
|
-
self.pages.append(_page)
|
|
649
|
+
_doc_pdf = DocumentPdf(f, lib_pdf=lib_pdf)
|
|
650
|
+
self.add_pages(_doc_pdf.to_pages())
|
|
621
651
|
del _doc_pdf
|
|
622
|
-
del _pages_pdf
|
|
623
652
|
|
|
624
653
|
def add_document(self, doc: DocumentPdf) -> None:
|
|
625
654
|
self.add_pages(doc.to_pages())
|
|
@@ -631,6 +660,18 @@ class CollectionPagePdf(object):
|
|
|
631
660
|
def set_land_scape(self):
|
|
632
661
|
for p in self.pages:
|
|
633
662
|
p.set_land_scape()
|
|
663
|
+
|
|
664
|
+
def to_document(self, lib_pdf: LibPDF = DEFAULT_LIB_PDF) -> DocumentPdf:
|
|
665
|
+
return DocumentPdf.create_from_pages(self.pages, lib_pdf=lib_pdf)
|
|
666
|
+
|
|
667
|
+
def to_file_pdf(self, file: File, *, replace: bool = False) -> None:
|
|
668
|
+
if not replace:
|
|
669
|
+
if file.exists():
|
|
670
|
+
print(f'[PULANDO]: o arquivo já existe {file.absolute()}')
|
|
671
|
+
return
|
|
672
|
+
_doc = DocumentPdf.create_from_pages(self.pages)
|
|
673
|
+
_doc.to_file(file)
|
|
674
|
+
del _doc
|
|
634
675
|
|
|
635
676
|
def to_files_pdf(
|
|
636
677
|
self,
|
|
@@ -7,18 +7,18 @@ from __future__ import annotations
|
|
|
7
7
|
from abc import ABC, abstractmethod
|
|
8
8
|
from convert_stream.enum_libs.enums import LibPDF, ColumnsTable
|
|
9
9
|
from convert_stream.enum_libs.modules import (
|
|
10
|
-
|
|
10
|
+
ModPagePdf, PageObject, fitz
|
|
11
11
|
)
|
|
12
12
|
from convert_stream.text.string import FindText, ArrayString, MapText
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
class ABCPagePdf(ABC):
|
|
16
16
|
|
|
17
|
-
def __init__(self, mod_page:
|
|
17
|
+
def __init__(self, mod_page: ModPagePdf, page_number: int):
|
|
18
18
|
if page_number < 1:
|
|
19
19
|
raise ValueError('page_number must be >= 1')
|
|
20
|
-
self.mod_page:
|
|
21
|
-
self.lib_pdf: LibPDF = LibPDF.
|
|
20
|
+
self.mod_page: ModPagePdf = mod_page
|
|
21
|
+
self.lib_pdf: LibPDF = LibPDF.NOT_IMPLEMENTED
|
|
22
22
|
self.number_page: int = page_number
|
|
23
23
|
|
|
24
24
|
@abstractmethod
|
|
@@ -72,21 +72,21 @@ class ABCPagePdf(ABC):
|
|
|
72
72
|
pass
|
|
73
73
|
|
|
74
74
|
@classmethod
|
|
75
|
-
def create_from_pypdf(cls, page: PageObject) -> ABCPagePdf:
|
|
75
|
+
def create_from_pypdf(cls, page: PageObject, number: int) -> ABCPagePdf:
|
|
76
76
|
pass
|
|
77
77
|
|
|
78
78
|
@classmethod
|
|
79
|
-
def create_from_fitz(cls, page: fitz.Page) -> ABCPagePdf:
|
|
79
|
+
def create_from_fitz(cls, page: fitz.Page, number: int) -> ABCPagePdf:
|
|
80
80
|
pass
|
|
81
81
|
|
|
82
82
|
|
|
83
83
|
class ImplementPypdf(ABCPagePdf):
|
|
84
84
|
|
|
85
|
-
def __init__(self, mod_page:
|
|
85
|
+
def __init__(self, mod_page: ModPagePdf, page_number: int):
|
|
86
86
|
super().__init__(mod_page, page_number)
|
|
87
87
|
self.lib_pdf: LibPDF = LibPDF.PYPDF
|
|
88
88
|
self.number_page: int = page_number
|
|
89
|
-
self.mod_page:
|
|
89
|
+
self.mod_page: ModPagePdf = mod_page
|
|
90
90
|
|
|
91
91
|
def set_rotation(self, num: int):
|
|
92
92
|
try:
|
|
@@ -163,7 +163,6 @@ class ImplementPypdf(ABCPagePdf):
|
|
|
163
163
|
text_page = self.to_string()
|
|
164
164
|
if (text_page is None) or (text_page == '') or (text_page == 'nas'):
|
|
165
165
|
return None
|
|
166
|
-
|
|
167
166
|
fd = FindText(text_page, separator)
|
|
168
167
|
return fd.find(text, iqual=iqual, case=case)
|
|
169
168
|
|
|
@@ -175,14 +174,13 @@ class ImplementPypdf(ABCPagePdf):
|
|
|
175
174
|
return fd.find_all(text, iqual=iqual, case=case)
|
|
176
175
|
|
|
177
176
|
@classmethod
|
|
178
|
-
def create_from_pypdf(cls, page:
|
|
179
|
-
|
|
180
|
-
return ImplementPypdf(mod_page, mod_page.page_number + 1)
|
|
177
|
+
def create_from_pypdf(cls, page: PageObject, number: int) -> ImplementPypdf:
|
|
178
|
+
return cls(page, number)
|
|
181
179
|
|
|
182
180
|
|
|
183
181
|
class ImplementFitz(ABCPagePdf):
|
|
184
182
|
|
|
185
|
-
def __init__(self, mod_page:
|
|
183
|
+
def __init__(self, mod_page: ModPagePdf, page_number: int):
|
|
186
184
|
super().__init__(mod_page, page_number)
|
|
187
185
|
self.lib_pdf: LibPDF = LibPDF.FITZ
|
|
188
186
|
self.number_page: int = page_number
|
|
@@ -276,8 +274,8 @@ class ImplementFitz(ABCPagePdf):
|
|
|
276
274
|
return fd.find_all(text, iqual=iqual, case=case)
|
|
277
275
|
|
|
278
276
|
@classmethod
|
|
279
|
-
def create_from_fitz(cls, page: fitz.Page) -> ABCPagePdf:
|
|
280
|
-
return cls(page,
|
|
277
|
+
def create_from_fitz(cls, page: fitz.Page, number: int) -> ABCPagePdf:
|
|
278
|
+
return cls(page, number)
|
|
281
279
|
|
|
282
280
|
|
|
283
281
|
class PageDocumentPdf(object):
|
|
@@ -343,15 +341,13 @@ class PageDocumentPdf(object):
|
|
|
343
341
|
self.implement_page_pdf.set_rotation(num)
|
|
344
342
|
|
|
345
343
|
@classmethod
|
|
346
|
-
def create_from_page_pypdf(cls, page:
|
|
347
|
-
|
|
348
|
-
return cls(p)
|
|
344
|
+
def create_from_page_pypdf(cls, page: PageObject, number: int) -> PageDocumentPdf:
|
|
345
|
+
return cls(ImplementPypdf(page, number))
|
|
349
346
|
|
|
350
347
|
@classmethod
|
|
351
|
-
def create_from_page_fitz(cls, page:
|
|
348
|
+
def create_from_page_fitz(cls, page: fitz.Page, number: int) -> PageDocumentPdf:
|
|
352
349
|
if page is None:
|
|
353
350
|
raise ValueError(f'fitz.Page não pode ser None')
|
|
354
|
-
|
|
355
|
-
return cls(p)
|
|
351
|
+
return cls(ImplementFitz(page, number))
|
|
356
352
|
|
|
357
353
|
|
|
@@ -5,13 +5,26 @@
|
|
|
5
5
|
"""
|
|
6
6
|
from __future__ import annotations
|
|
7
7
|
from abc import ABC, abstractmethod
|
|
8
|
-
from soup_files import
|
|
8
|
+
from soup_files import Directory, ProgressBarAdapter, CreatePbar
|
|
9
9
|
from convert_stream.enum_libs.enums import LibPdfToImage
|
|
10
10
|
from convert_stream.enum_libs.modules import DEFAULT_LIB_PDF_TO_IMG, DEFAULT_LIB_IMAGE
|
|
11
11
|
from convert_stream.image.img_object import ImageObject, LibImage, CollectionImages
|
|
12
|
-
from convert_stream.pdf.pdf_page import PageDocumentPdf
|
|
12
|
+
from convert_stream.pdf.pdf_page import PageDocumentPdf
|
|
13
13
|
from convert_stream.pdf.pdf_document import DocumentPdf
|
|
14
14
|
|
|
15
|
+
try:
|
|
16
|
+
import fitz
|
|
17
|
+
MOD_FITZ = True
|
|
18
|
+
except ImportError:
|
|
19
|
+
try:
|
|
20
|
+
import pymupdf as fitz
|
|
21
|
+
from pymupdf import Page, Pixmap
|
|
22
|
+
MOD_FITZ = True
|
|
23
|
+
except ImportError:
|
|
24
|
+
fitz = object
|
|
25
|
+
fitz.Page = object
|
|
26
|
+
fitz.TextPage = object
|
|
27
|
+
|
|
15
28
|
|
|
16
29
|
class ABCConvertPdf(ABC):
|
|
17
30
|
|
|
@@ -57,48 +70,36 @@ class ImplementConvertPdfFitz(ABCConvertPdf):
|
|
|
57
70
|
|
|
58
71
|
def __init__(self, document: DocumentPdf):
|
|
59
72
|
super().__init__(document)
|
|
60
|
-
self.lib_pdf_to_image: LibPdfToImage = LibPdfToImage.PDF_TO_IMG_FITZ
|
|
61
|
-
self.__collection_imgs: CollectionImages = CollectionImages()
|
|
62
|
-
self.__collection_imgs.set_pbar(self.pbar)
|
|
63
73
|
|
|
64
|
-
def
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
Extrair as imagens do documento e salvar na propriedade CollectionImages()
|
|
71
|
-
"""
|
|
72
|
-
if not self.__collection_imgs.is_empty:
|
|
73
|
-
return
|
|
74
|
+
def __get_pages(self) -> list[Page]:
|
|
75
|
+
pages = self.document.to_pages()
|
|
76
|
+
fitz_pages = []
|
|
77
|
+
for p in pages:
|
|
78
|
+
fitz_pages.append(p.implement_page_pdf.mod_page)
|
|
79
|
+
return fitz_pages
|
|
74
80
|
|
|
75
|
-
|
|
81
|
+
def to_images(
|
|
82
|
+
self, *,
|
|
83
|
+
dpi: int = 200,
|
|
84
|
+
lib_image: LibImage = DEFAULT_LIB_IMAGE
|
|
85
|
+
) -> list[ImageObject]:
|
|
86
|
+
images: list[ImageObject] = []
|
|
87
|
+
pages_fitz: list[Page] = self.__get_pages()
|
|
88
|
+
_count = len(pages_fitz)
|
|
76
89
|
print()
|
|
77
|
-
|
|
78
|
-
matrix = fitz.Matrix(zoom, zoom)
|
|
79
|
-
pages_pdf: list[PageDocumentPdf] = self.document.to_pages()
|
|
80
|
-
max_num: int = len(pages_pdf)
|
|
81
|
-
for num, current_page in enumerate(pages_pdf):
|
|
90
|
+
for n, pg in enumerate(pages_fitz):
|
|
82
91
|
self.pbar.update(
|
|
83
|
-
((
|
|
84
|
-
f'
|
|
92
|
+
((n+1)/_count) * 100,
|
|
93
|
+
f'Convetendo: {n+1} de {_count}'
|
|
94
|
+
)
|
|
95
|
+
pix: Pixmap = pg.get_pixmap(dpi=dpi)
|
|
96
|
+
img = ImageObject.create_from_bytes(
|
|
97
|
+
pix.tobytes('png', jpg_quality=100),
|
|
98
|
+
lib_image=LibImage.PIL,
|
|
85
99
|
)
|
|
86
|
-
|
|
87
|
-
pix: fitz.Pixmap = page.get_pixmap(matrix=matrix, alpha=False) # sem canal alpha
|
|
88
|
-
img_bytes = pix.tobytes("png")
|
|
89
|
-
img_obj = ImageObject.create_from_bytes(img_bytes, lib_image=lib_image)
|
|
90
|
-
#img_obj = ImageObject.create_from_pil(pix.pil_image())
|
|
91
|
-
self.__collection_imgs.add_image(img_obj)
|
|
100
|
+
images.append(img)
|
|
92
101
|
print()
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def to_images(
|
|
96
|
-
self, *,
|
|
97
|
-
dpi: int = 300,
|
|
98
|
-
lib_image: LibImage = DEFAULT_LIB_IMAGE,
|
|
99
|
-
) -> list[ImageObject]:
|
|
100
|
-
self.__extract_images(dpi=dpi, lib_image=lib_image)
|
|
101
|
-
return self.__collection_imgs.images
|
|
102
|
+
return images
|
|
102
103
|
|
|
103
104
|
def to_files_image(
|
|
104
105
|
self,
|
|
@@ -106,19 +107,28 @@ class ImplementConvertPdfFitz(ABCConvertPdf):
|
|
|
106
107
|
replace: bool = False,
|
|
107
108
|
gaussian_filter: bool = False,
|
|
108
109
|
dpi: int = 300,
|
|
109
|
-
lib_image: LibImage = DEFAULT_LIB_IMAGE
|
|
110
|
+
lib_image: LibImage = DEFAULT_LIB_IMAGE
|
|
110
111
|
) -> None:
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
112
|
+
pages_fitz: list[Page] = self.__get_pages()
|
|
113
|
+
_count = len(pages_fitz)
|
|
114
|
+
print()
|
|
115
|
+
for n, pg in enumerate(pages_fitz):
|
|
116
|
+
self.pbar.update(
|
|
117
|
+
((n + 1) / _count) * 100,
|
|
118
|
+
f'Exportando: {n + 1} de {_count}'
|
|
119
|
+
)
|
|
120
|
+
out_file = output_dir.join_file(f'pag-{n+1}.png')
|
|
121
|
+
if not replace:
|
|
122
|
+
if out_file.exists():
|
|
123
|
+
self.pbar.update_text(f'Pulando: {out_file.basename()}')
|
|
124
|
+
continue
|
|
125
|
+
pix: Pixmap = pg.get_pixmap(dpi=dpi)
|
|
126
|
+
img = ImageObject.create_from_bytes(
|
|
127
|
+
pix.tobytes('png', jpg_quality=100),
|
|
128
|
+
lib_image=LibImage.PIL,
|
|
129
|
+
)
|
|
130
|
+
img.to_file(out_file)
|
|
131
|
+
print()
|
|
122
132
|
|
|
123
133
|
|
|
124
134
|
class ConvertPdfToImages(object):
|
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
3
|
from __future__ import annotations
|
|
4
|
-
import sys
|
|
5
4
|
from abc import ABC, abstractmethod
|
|
6
|
-
from typing import Optional, List
|
|
7
5
|
from openpyxl import Workbook, load_workbook
|
|
8
6
|
from openpyxl.worksheet._read_only import ReadOnlyWorksheet
|
|
9
7
|
from pandas.io.parsers import TextFileReader
|
|
10
8
|
import pandas as pd
|
|
11
9
|
import csv
|
|
12
|
-
from soup_files import File,
|
|
13
|
-
from soup_files.files import ExtensionFiles
|
|
10
|
+
from soup_files import File, ProgressBarAdapter, CreatePbar
|
|
14
11
|
|
|
15
12
|
|
|
16
13
|
class ABCSheetReader(ABC):
|
|
17
14
|
"""Classe abstrata para leitura de planilhas CSV e EXCEL"""
|
|
18
15
|
|
|
19
16
|
def __init__(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
self,
|
|
18
|
+
file: File, *,
|
|
19
|
+
pbar: ProgressBarAdapter = CreatePbar().get()
|
|
20
|
+
):
|
|
24
21
|
self.pbar: ProgressBarAdapter = pbar
|
|
25
22
|
self.file: File = file
|
|
26
23
|
self.df = None
|
|
@@ -117,8 +114,8 @@ class ReaderCsv(ABCSheetReader):
|
|
|
117
114
|
total_lines = self.get_total_rows()
|
|
118
115
|
chunks_text: list[TextFileReader] = []
|
|
119
116
|
for num, chunk in enumerate(
|
|
120
|
-
|
|
121
|
-
|
|
117
|
+
pd.read_csv(self.file.absolute(), chunksize=1000, sep=self.separator),
|
|
118
|
+
):
|
|
122
119
|
percent: float = (((num + 1) * 1000) / total_lines) * 100
|
|
123
120
|
self.pbar.update(percent, f"Lendo CSV [{self.file.basename()}]")
|
|
124
121
|
chunks_text.append(chunk)
|
|
@@ -138,9 +135,9 @@ class ReaderCsv(ABCSheetReader):
|
|
|
138
135
|
class ReaderExcel(ABCSheetReader):
|
|
139
136
|
|
|
140
137
|
def __init__(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
138
|
+
self, file: File, *,
|
|
139
|
+
pbar: ProgressBarAdapter = CreatePbar().get()
|
|
140
|
+
):
|
|
144
141
|
super().__init__(file, pbar=pbar)
|
|
145
142
|
self.df = pd.DataFrame()
|
|
146
143
|
|
|
@@ -315,10 +312,10 @@ class ReaderOds(ABCSheetReader):
|
|
|
315
312
|
class ReadFileSheet(object):
|
|
316
313
|
|
|
317
314
|
def __init__(
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
315
|
+
self, file: File, *,
|
|
316
|
+
separator: str = '\t',
|
|
317
|
+
pbar: ProgressBarAdapter = CreatePbar().get()
|
|
318
|
+
):
|
|
322
319
|
if file.is_csv():
|
|
323
320
|
self.sheet_reader: ABCSheetReader = ReaderCsv(file, pbar=pbar, separator=separator)
|
|
324
321
|
elif file.is_excel():
|
|
@@ -345,5 +342,3 @@ class ReadFileSheet(object):
|
|
|
345
342
|
|
|
346
343
|
def get_dataframe(self, sheet_name: str = None, *, progress: bool = True) -> pd.DataFrame:
|
|
347
344
|
return self.sheet_reader.get_dataframe(sheet_name, progress=progress)
|
|
348
|
-
|
|
349
|
-
|
|
@@ -9,7 +9,7 @@ from soup_files import (
|
|
|
9
9
|
from convert_stream.enum_libs.enums import ColumnsTable
|
|
10
10
|
from convert_stream.text.string import FindText, ArrayString, MapText
|
|
11
11
|
from convert_stream.pdf.pdf_document import SearchableTextPdf, DocumentPdf
|
|
12
|
-
from convert_stream.
|
|
12
|
+
from convert_stream.doc_stream import PdfStream, CollectionPagePdf
|
|
13
13
|
import pandas as pd
|
|
14
14
|
|
|
15
15
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: convert_stream
|
|
3
|
-
Version: 2.2.
|
|
3
|
+
Version: 2.2.7
|
|
4
4
|
Summary: Manipulação de documentos PDF e Imagens. Converter, Ler, rotacionar exportar etc.
|
|
5
5
|
Author: Bruno Chaves
|
|
6
6
|
Requires-Python: >=3.11
|
|
@@ -24,12 +24,12 @@ pip3 install -r requirements.txt
|
|
|
24
24
|
>>> from file import File, Directory
|
|
25
25
|
>>> from pdflib import PdfStream
|
|
26
26
|
# instâncie o caminho do arquivo PDF e o diretório para exportar os dados.
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
file_pdf = File('path/to/file.pdf')
|
|
28
|
+
output_dir = Directory('path/to/save')
|
|
29
29
|
|
|
30
30
|
pdf_stream = PdfStream()
|
|
31
|
-
pdf_stream.add_file_pdf(
|
|
32
|
-
pdf_stream.to_files_pdf(
|
|
31
|
+
pdf_stream.add_file_pdf(file_pdf)
|
|
32
|
+
pdf_stream.to_files_pdf(output_dir) # isso irá converter cada página em um arquivo PDF
|
|
33
33
|
|
|
34
34
|
# Uso para unir vários arquivos PDF.
|
|
35
35
|
>>> from file import File, Directory
|
|
@@ -3,13 +3,14 @@ pyproject.toml
|
|
|
3
3
|
convert_stream/__init__.py
|
|
4
4
|
convert_stream/__main__.py
|
|
5
5
|
convert_stream/_version.py
|
|
6
|
-
convert_stream/
|
|
6
|
+
convert_stream/doc_stream.py
|
|
7
7
|
convert_stream/table_files.py
|
|
8
8
|
convert_stream.egg-info/PKG-INFO
|
|
9
9
|
convert_stream.egg-info/SOURCES.txt
|
|
10
10
|
convert_stream.egg-info/dependency_links.txt
|
|
11
11
|
convert_stream.egg-info/requires.txt
|
|
12
12
|
convert_stream.egg-info/top_level.txt
|
|
13
|
+
convert_stream/enum_libs/__init__.py
|
|
13
14
|
convert_stream/enum_libs/enums.py
|
|
14
15
|
convert_stream/enum_libs/modules.py
|
|
15
16
|
convert_stream/image/__init__.py
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
|
|
3
|
-
from ._version import (
|
|
4
|
-
__module_name__, __author__, __license__, __modify_date__, __version__
|
|
5
|
-
)
|
|
6
|
-
|
|
7
|
-
from convert_stream.enum_libs.enums import *
|
|
8
|
-
from convert_stream.enum_libs.modules import (
|
|
9
|
-
LibPDF, LibDate, LibImage, LibPdfToImage, LibImageToPdf,
|
|
10
|
-
DEFAULT_LIB_PDF, DEFAULT_LIB_IMAGE, DEFAULT_LIB_PDF_TO_IMG,
|
|
11
|
-
DEFAULT_LIB_IMAGE_TO_PDF, ModPdfToImage, ModulePagePdf, ModuleImage,
|
|
12
|
-
ModImageToPdf, MOD_FITZ, MOD_PYPDF, MOD_CANVAS, MOD_IMG_PIL, MOD_IMG_OPENCV,
|
|
13
|
-
)
|
|
14
|
-
from .text.string import (
|
|
15
|
-
FindText, ArrayString, print_line, print_title, MapText
|
|
16
|
-
)
|
|
17
|
-
from .text.date import ConvertStringDate
|
|
18
|
-
from convert_stream.image.img_object import (
|
|
19
|
-
ImageObject, Image, MatLike, get_hash_from_bytes, CollectionImages,
|
|
20
|
-
)
|
|
21
|
-
from convert_stream.pdf import (
|
|
22
|
-
PageDocumentPdf, DocumentPdf, ConvertImageToPdf, ConvertPdfToImages,
|
|
23
|
-
ModImageToPdf, ModuleDocPdf, CollectionPagePdf, DEFAULT_LIB_PDF,
|
|
24
|
-
DEFAULT_LIB_IMAGE_TO_PDF, DEFAULT_LIB_PDF_TO_IMG
|
|
25
|
-
)
|
|
26
|
-
from .stream import PdfStream, SplitPdf
|
|
27
|
-
from .sheets import save_data, ReadFileSheet
|
|
28
|
-
from .table_files import (
|
|
29
|
-
get_void_df, create_df_from_file_pdf, create_map_from_values,
|
|
30
|
-
FileToTable, ColumnsTable, PdfFinder, SearchableTextPdf
|
|
31
|
-
)
|
|
File without changes
|
/convert_stream-2.2.5/convert_stream/stream.py → /convert_stream-2.2.7/convert_stream/doc_stream.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|