@@ -68,7 +68,7 @@ After creating and activating the virtual environment, **install** required **li
68
68
pip install "fastapi[standard]" docx docxtpl pydantic requests
69
69
70
70
Then, Create a file “** main.py”** with:
71
-
71
+ ``` python
72
72
from fastapi import FastAPI
73
73
74
74
app = FastAPI()
@@ -77,21 +77,21 @@ Then, Create a file “**main.py”** with:
77
77
@app.get (" /" )
78
78
def read_root ():
79
79
return {" Hello" : " World" }
80
-
80
+ ```
81
81
If you make a ** request** (using Postman, or your browser) to *** “localhost:8000”*** , you should get this as the output:
82
-
82
+ ``` python
83
83
{
84
84
" Hello" : " World"
85
85
}
86
-
86
+ ```
87
87
Now that we have a basic endpoint running, we can start rendering context into Word template file.
88
88
89
89
Import your ** Jinja2** modified Word template file into the root of your project like so:
90
90
91
91
![ ] ( https://cdn-images-1.medium.com/max/2000/1*IBOIVtjqQ2i1lCoRI9d1aQ.png )
92
92
93
93
Modify the main.py file to be:
94
-
94
+ ``` python
95
95
from fastapi import FastAPI
96
96
from docxtpl import DocxTemplate, InlineImage
97
97
from pydantic import BaseModel
@@ -198,7 +198,7 @@ Modify the main.py file to be:
198
198
return StreamingResponse(result,
199
199
media_type = " application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
200
200
headers = {" Content-Disposition" : " attachment; filename=invoice.docx" })
201
-
201
+ ```
202
202
Now, let’s test the endpoint by sending a ** JSON body** and ** retrieve** the ** generated output** .
203
203
204
204
{
0 commit comments