-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare 0.16.3. Add section for docx creation.
- Loading branch information
1 parent
d55d0ca
commit 04dc099
Showing
13 changed files
with
174 additions
and
401 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
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,91 @@ | ||
Creating DOCX | ||
============= | ||
|
||
Building DOCX | ||
------------- | ||
See the following full functional snippet for generating DOCX. | ||
|
||
.. code-block:: python | ||
# Imports | ||
from faker import Faker | ||
from faker_file.providers.docx_file import DocxFileProvider | ||
FAKER = Faker() # Initialize Faker | ||
FAKER.add_provider(DocxFileProvider) # Register DocxFileProvider | ||
# Generate DOCX file | ||
docx_file = FAKER.docx_file() | ||
The generated DOCX will have 10,000 characters of text, which is about 5 pages. | ||
|
||
If you want DOCX with more pages, you could either: | ||
|
||
- Increase the value of ``max_nb_chars`` accordingly. | ||
- Set value of ``wrap_chars_after`` to 80 characters to force longer pages. | ||
- Insert manual page breaks and other content. | ||
|
||
See the example below for ``max_nb_chars`` tweak: | ||
|
||
.. code-block:: python | ||
# Generate DOCX file of 20,000 characters | ||
docx_file = FAKER.docx_file(max_nb_chars=20_000) | ||
See the example below for ``wrap_chars_after`` tweak: | ||
|
||
.. code-block:: python | ||
# Generate DOCX file, wrapping each line after 80 characters | ||
docx_file = FAKER.docx_file(wrap_chars_after=80) | ||
As mentioned above, it's possible to diversify the generated context with | ||
images, paragraphs, tables, manual text break and pretty much everything that | ||
is supported by DOCX format specification, although currently only images, | ||
paragraphs, tables and manual text breaks are supported out of the box. In | ||
order to customise the blocks DOCX file is built from, the ``DynamicTemplate`` | ||
class is used. See the example below for usage examples: | ||
|
||
.. code-block:: python | ||
# Additional imports | ||
from faker_file.base import DynamicTemplate | ||
from faker_file.contrib.docx_file import ( | ||
add_page_break, | ||
add_paragraph, | ||
add_picture, | ||
add_table, | ||
) | ||
# Create a DOCX file with paragraph, picture, table and manual page breaks | ||
# in between the mentioned elements. The ``DynamicTemplate`` simply | ||
# accepts a list of callables (such as ``add_paragraph``, | ||
# ``add_page_break``) and dictionary to be later on fed to the callables | ||
# as keyword arguments for customising the default values. | ||
docx_file = FAKER.docx_file( | ||
content=DynamicTemplate( | ||
[ | ||
(add_paragraph, {}), # Add paragraph | ||
(add_page_break, {}), # Add page break | ||
(add_picture, {}), # Add picture | ||
(add_page_break, {}), # Add page break | ||
(add_table, {}), # Add table | ||
(add_page_break, {}), # Add page break | ||
] | ||
) | ||
) | ||
# You could make the list as long as you like or simply multiply for | ||
# easier repetition as follows: | ||
docx_file = FAKER.docx_file( | ||
content=DynamicTemplate( | ||
[ | ||
(add_paragraph, {}), # Add paragraph | ||
(add_page_break, {}), # Add page break | ||
(add_picture, {}), # Add picture | ||
(add_page_break, {}), # Add page break | ||
(add_table, {}), # Add table | ||
(add_page_break, {}), # Add page break | ||
] * 100 # Will repeat your config 100 times | ||
) | ||
) |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ Contents: | |
quick_start | ||
recipes | ||
creating_pdfs | ||
creating_docx | ||
cli | ||
security | ||
code_of_conduct | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
__title__ = "faker_file" | ||
__version__ = "0.16.2" | ||
__version__ = "0.16.3" | ||
__author__ = "Artur Barseghyan <[email protected]>" | ||
__copyright__ = "2022-2023 Artur Barseghyan" | ||
__license__ = "MIT" |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
from io import BytesIO | ||
|
||
from ..base import DEFAULT_FORMAT_FUNC | ||
|
||
__author__ = "Artur Barseghyan <[email protected]>" | ||
__copyright__ = "2022-2023 Artur Barseghyan" | ||
__license__ = "MIT" | ||
__all__ = ( | ||
"add_page_break", | ||
"add_paragraph", | ||
"add_picture", | ||
"add_table", | ||
) | ||
|
@@ -47,3 +51,32 @@ def add_picture(provider, document, data, counter, **kwargs): | |
# jpeg_file.data["content"] | ||
# ) | ||
# data["content"] += "\r\n" + jpeg_file.data["content"] | ||
|
||
|
||
def add_page_break(provider, document, data, counter, **kwargs): | ||
"""Callable responsible for page break generation.""" | ||
# Insert a page break | ||
document.add_page_break() | ||
|
||
|
||
def add_paragraph(provider, document, data, counter, **kwargs): | ||
"""Callable responsible for the paragraph generation.""" | ||
content = kwargs.get("content", None) | ||
max_nb_chars = kwargs.get("content", 5_000) | ||
wrap_chars_after = kwargs.get("wrap_chars_after", None) | ||
format_func = kwargs.get("format_func", DEFAULT_FORMAT_FUNC) | ||
|
||
_content = provider._generate_text_content( | ||
max_nb_chars=max_nb_chars, | ||
wrap_chars_after=wrap_chars_after, | ||
content=content, | ||
format_func=format_func, | ||
) | ||
document.add_paragraph(_content) | ||
|
||
# Meta-data | ||
data.setdefault("content_modifiers", {}) | ||
data["content_modifiers"].setdefault("add_paragraph", {}) | ||
data["content_modifiers"]["add_paragraph"].setdefault(counter, []) | ||
data["content_modifiers"]["add_paragraph"][counter].append(_content) | ||
data["content"] += "\r\n" + _content |
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
Oops, something went wrong.