From 1290e6926e0268262a799150627c8f523a3d0902 Mon Sep 17 00:00:00 2001 From: Artur Barseghyan Date: Tue, 27 Jun 2023 23:52:16 +0200 Subject: [PATCH] Add docx and odt snippets --- pyproject.toml | 1 + src/faker_file/contrib/docx_file.py | 49 ++++++++++++++ src/faker_file/contrib/odt_file.py | 93 ++++++++++++++++++++++++++ src/faker_file/tests/test_providers.py | 26 +++---- 4 files changed, 152 insertions(+), 17 deletions(-) create mode 100644 src/faker_file/contrib/docx_file.py create mode 100644 src/faker_file/contrib/odt_file.py diff --git a/pyproject.toml b/pyproject.toml index 36c677f..75f87b7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ build-backend = "setuptools.build_meta" [tool.isort] profile = "black" +combine_as_imports = true multi_line_output = 3 include_trailing_comma = true force_grid_wrap = 0 diff --git a/src/faker_file/contrib/docx_file.py b/src/faker_file/contrib/docx_file.py new file mode 100644 index 0000000..f233f57 --- /dev/null +++ b/src/faker_file/contrib/docx_file.py @@ -0,0 +1,49 @@ +from io import BytesIO + +__author__ = "Artur Barseghyan " +__copyright__ = "2022-2023 Artur Barseghyan" +__license__ = "MIT" +__all__ = ( + "add_picture", + "add_table", +) + + +def add_table(provider, document, data, counter, **kwargs): + """Callable responsible for the table generation.""" + table = document.add_table( + kwargs.get("rows", 3), + kwargs.get("cols", 4), + ) + + # Modifications of `data` is not required for generation + # of the file, but is useful for when you want to get + # the text content of the file. + data.setdefault("content_modifiers", {}) + data["content_modifiers"].setdefault("add_table", {}) + data["content_modifiers"]["add_table"].setdefault(counter, []) + + for row in table.rows: + for cell in row.cells: + text = provider.generator.paragraph() + cell.text = text + # Useful when you want to get the text content of the file. + data["content_modifiers"]["add_table"][counter].append(text) + data["content"] += "\r\n" + text + + +def add_picture(provider, document, data, counter, **kwargs): + """Callable responsible for the picture generation.""" + png_raw = provider.generator.image() + document.add_picture(BytesIO(png_raw)) + + # # Modifications of `data` is not required for generation + # # of the file, but is useful for when you want to get + # # the text content of the file. + # data.setdefault("content_modifiers", {}) + # data["content_modifiers"].setdefault("add_picture", {}) + # data["content_modifiers"]["add_picture"].setdefault(counter, []) + # data["content_modifiers"]["add_picture"][counter].append( + # jpeg_file.data["content"] + # ) + # data["content"] += "\r\n" + jpeg_file.data["content"] diff --git a/src/faker_file/contrib/odt_file.py b/src/faker_file/contrib/odt_file.py new file mode 100644 index 0000000..1ef1a2b --- /dev/null +++ b/src/faker_file/contrib/odt_file.py @@ -0,0 +1,93 @@ +from odf.draw import Frame, Image +from odf.style import ( + Style, + TableCellProperties, + TableColumnProperties, + TableRowProperties, +) +from odf.table import Table, TableCell, TableColumn, TableRow +from odf.text import P + +__author__ = "Artur Barseghyan " +__copyright__ = "2022-2023 Artur Barseghyan" +__license__ = "MIT" +__all__ = ( + "add_picture", + "add_table", +) + + +def add_table(provider, document, data, counter, **kwargs): + """Callable responsible for the table generation.""" + rows = kwargs.get("rows", 3) + cols = kwargs.get("cols", 4) + table_col_style = Style(name="TableColumn", family="table-column") + table_col_style.addElement(TableColumnProperties(columnwidth="2cm")) + document.automaticstyles.addElement(table_col_style) + + table_row_style = Style(name="TableRow", family="table-row") + table_row_style.addElement(TableRowProperties(rowheight="1cm")) + document.automaticstyles.addElement(table_row_style) + + table_cell_style = Style(name="TableCell", family="table-cell") + table_cell_style.addElement( + TableCellProperties(padding="0.1cm", border="0.05cm solid #000000") + ) + document.automaticstyles.addElement(table_cell_style) + + # Modifications of `data` is not required for generation + # of the file, but is useful for when you want to get + # the text content of the file. + data.setdefault("content_modifiers", {}) + data["content_modifiers"].setdefault("add_table", {}) + data["content_modifiers"]["add_table"].setdefault(counter, []) + + # Create table + table = Table() + for i in range(rows): + table.addElement(TableColumn(stylename=table_col_style)) + + for row in range(cols): + tr = TableRow(stylename=table_row_style) + table.addElement(tr) + for col in range(4): + tc = TableCell(stylename=table_cell_style) + tr.addElement(tc) + text = provider.generator.paragraph() + p = P(text=text) + tc.addElement(p) + # Useful when you want to get the text content of the file. + data["content_modifiers"]["add_table"][counter].append(text) + data["content"] += "\r\n" + text + + document.text.addElement(table) + + +def add_picture(provider, document, data, counter, **kwargs): + """Callable responsible for the picture generation.""" + width = kwargs.get("width", "10cm") + height = kwargs.get("height", "5cm") + paragraph = P() + document.text.addElement(paragraph) + png_raw = provider.generator.image() + image_frame = Frame( + width=width, + height=height, + x="56pt", + y="56pt", + anchortype="paragraph", + ) + href = document.addPicture(filename="image.png", content=png_raw) + image_frame.addElement(Image(href=href)) + paragraph.addElement(image_frame) + + # # Modifications of `data` is not required for generation + # # of the file, but is useful for when you want to get + # # the text content of the file. + # data["content"] += "\r\n" + jpeg_file.data["content"] + # data.setdefault("content_modifiers", {}) + # data["content_modifiers"].setdefault("add_picture", {}) + # data["content_modifiers"]["add_picture"].setdefault(counter, []) + # data["content_modifiers"]["add_picture"][counter].append( + # jpeg_file.data["content"] + # ) diff --git a/src/faker_file/tests/test_providers.py b/src/faker_file/tests/test_providers.py index 3e1e4eb..bace628 100644 --- a/src/faker_file/tests/test_providers.py +++ b/src/faker_file/tests/test_providers.py @@ -17,26 +17,24 @@ DEFAULT_FONT_PATH, DEFAULT_TEXT_CONTENT_TEMPLATE, ) -from ..contrib.pdf_file.pdfkit_snippets import ( - add_page_break as pdf_pdfkit_add_page_break, +from ..contrib.docx_file import ( + add_picture as docx_add_picture, + add_table as docx_add_table, ) -from ..contrib.pdf_file.pdfkit_snippets import ( - add_paragraph as pdf_pdfkit_add_paragraph, +from ..contrib.odt_file import ( + add_picture as odt_add_picture, + add_table as odt_add_table, ) from ..contrib.pdf_file.pdfkit_snippets import ( + add_page_break as pdf_pdfkit_add_page_break, + add_paragraph as pdf_pdfkit_add_paragraph, add_picture as pdf_pdfkit_add_picture, + add_table as pdf_pdfkit_add_table, ) -from ..contrib.pdf_file.pdfkit_snippets import add_table as pdf_pdfkit_add_table from ..contrib.pdf_file.reportlab_snippets import ( add_page_break as pdf_reportlab_add_page_break, -) -from ..contrib.pdf_file.reportlab_snippets import ( add_paragraph as pdf_reportlab_add_paragraph, -) -from ..contrib.pdf_file.reportlab_snippets import ( add_picture as pdf_reportlab_add_picture, -) -from ..contrib.pdf_file.reportlab_snippets import ( add_table as pdf_reportlab_add_table, ) from ..helpers import load_class_from_path @@ -113,12 +111,6 @@ XML_ISBN_KWARGS, XML_METADATA_KWARGS, ) -from .helpers import ( - docx_add_picture, - docx_add_table, - odt_add_picture, - odt_add_table, -) from .texts import TEXT_PDF __author__ = "Artur Barseghyan "