Skip to content

Commit

Permalink
Update automating-word-documents.md
Browse files Browse the repository at this point in the history
  • Loading branch information
huseink authored Dec 23, 2024
1 parent 050ab27 commit 3296664
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/pages/blog/automating-word-documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ After creating and activating the virtual environment, **install** required **li
pip install "fastapi[standard]" docx docxtpl pydantic requests

Then, Create a file “**main.py”** with:

```python
from fastapi import FastAPI

app = FastAPI()
Expand All @@ -77,21 +77,21 @@ Then, Create a file “**main.py”** with:
@app.get("/")
def read_root():
return {"Hello": "World"}

```
If you make a **request** (using Postman, or your browser) to ***“localhost:8000”***, you should get this as the output:

```python
{
"Hello": "World"
}

```
Now that we have a basic endpoint running, we can start rendering context into Word template file.

Import your **Jinja2** modified Word template file into the root of your project like so:

![](https://cdn-images-1.medium.com/max/2000/1*IBOIVtjqQ2i1lCoRI9d1aQ.png)

Modify the main.py file to be:

```python
from fastapi import FastAPI
from docxtpl import DocxTemplate, InlineImage
from pydantic import BaseModel
Expand Down Expand Up @@ -198,7 +198,7 @@ Modify the main.py file to be:
return StreamingResponse(result,
media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
headers={"Content-Disposition": "attachment; filename=invoice.docx"})

```
Now, let’s test the endpoint by sending a **JSON body** and **retrieve** the **generated output**.

{
Expand Down

0 comments on commit 3296664

Please sign in to comment.