My first paid project.
chatgpt made docs To document the provided FastAPI application that fills in a contract template, you can follow these steps:
Make sure you have the following libraries installed:
fastapi
python-docx
You can install them using pip:
pip install fastapi python-docx
Here's a breakdown of the code:
-
Imports: Import necessary libraries and modules.
-
FastAPI Setup: Initialize a FastAPI instance (
app
). -
CORS Middleware: Allow CORS for POST requests to enable cross-origin requests.
-
fill_contract_template Function: This function takes
contract_details
as input, replaces placeholders in a Word document template (tomp.docx
) with corresponding values fromcontract_details
, and returns aBytesIO
stream of the filled document. -
POST Endpoint
/fill-contract
: Defines a POST endpoint/fill-contract
that accepts a JSON body (contract_details
) containing the data to fill into the contract template. It callsfill_contract_template
to get the filled document as a stream and returns it as aStreamingResponse
with media typeapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
. -
Main Execution: Runs the FastAPI application using Uvicorn server.
To use this API:
- Ensure
tomp.docx
exists in the same directory as your script, containing placeholders like{key}
that match the keys incontract_details
. - Send a POST request to
http://127.0.0.1:8000/fill-contract
with JSON payload containing your contract details.
import requests
# Endpoint URL
url = 'http://127.0.0.1:8000/fill-contract'
# Example contract details
contract_details = {
'client_name': 'John Doe',
'company_name': 'ABC Inc.',
'start_date': '2024-07-05',
'end_date': '2024-12-31',
'amount': 5000.00,
}
# Send POST request with JSON payload
response = requests.post(url, json=contract_details)
# Check response status
if response.status_code == 200:
# Save the filled contract to a file
with open('filled_contract.docx', 'wb') as f:
f.write(response.content)
print("Filled contract saved successfully.")
else:
print(f"Failed to fill contract. Status code: {response.status_code}")
this was sined on {start_date} and end at {end_date} client_name: {client_name} company_name: {company_name} amount: {amount}
the code ubove works for the tomp example the code changes the stuff on the {}