# Creating DOCX The goal of this library is to help you out with basic tasks. You can easily generate DOCX files with 100 pages (paper size is A4), having a little text on each. If you don’t like how DOCX files are generated by this library, you can check the [faker-file](https://pypi.org/project/faker-file/) package, which can produce complex DOCX documents. ## If you need bytes ```python from fake import FAKER docx_bytes = FAKER.docx() ``` *See the full example* [`here`](_static/examples/creating_docx/docx_bytes_1.py) The generated DOCX will consist of a single page with little text on it. If you want to control the number of pages created, you could: - Pass a list of texts to the `texts` argument. - Pass the number of pages to the `nb_pages` argument. --- See the example below for `texts` tweak: ```python texts = FAKER.sentences() docx_bytes = FAKER.docx(texts=texts) ``` *See the full example* [`here`](_static/examples/creating_docx/docx_bytes_2.py) --- See the example below for `nb_pages` tweak: ```python docx_bytes = FAKER.docx(nb_pages=100) ``` *See the full example* [`here`](_static/examples/creating_docx/docx_bytes_3.py) ## If you need files ```python docx_file = FAKER.docx_file() ``` *See the full example* [`here`](_static/examples/creating_docx/docx_file_1.py) --- With `texts` tweak: ```python texts = FAKER.sentences() docx_file = FAKER.docx_file(texts=texts) ``` *See the full example* [`here`](_static/examples/creating_docx/docx_file_2.py) --- With `nb_pages` tweak: ```python docx_file = FAKER.docx_file(nb_pages=100) ``` *See the full example* [`here`](_static/examples/creating_docx/docx_file_3.py) --- Using text templates: ```python from fake import FAKER, StringTemplate template = """ {date(start_date='-7d')} {name} {sentence(nb_words=2, suffix='')} {pyint(min_value=1, max_value=99)} {randomise_string(value='#### ??', digits='123456789')} {city} Dear friend, {text(nb_chars=1000, allow_overflow=True)} Sincerely yours, {name} {email} {domain_name} """ # DOCX file of 1 page docx_file_1 = FAKER.docx_file( texts=[StringTemplate(template)], ) # DOCX file of 10 pages docx_file_10 = FAKER.docx_file( texts=[StringTemplate(template) for _ in range(10)], ) # Tests assert isinstance(docx_file_1, str) assert docx_file_1.data["storage"].exists(docx_file_1) assert isinstance(docx_file_10, str) assert docx_file_10.data["storage"].exists(docx_file_10) ``` ---