doc_generator2 / app.py
Borya-Goldarb's picture
update
c03a40b verified
import streamlit as st
from docx import Document
from docx2pdf import convert
import datetime
# Function to load file based on selected filename
def load_file(filename):
with open(filename, 'r') as file:
file_contents = file.read()
return file_contents
# Function to save file with changes
def save_file(filename, content):
with open(filename, 'w') as file:
file.write(content)
# Function to create 2 formats of current date "Month DD YYYY" "MM/DD/YYYY"
def create_date_format():
current_date = datetime.datetime.now()
current_month = current_date.month
current_day = current_date.day
current_year = current_date.year
month_name = datetime.datetime.strptime(str(current_month), "%m").strftime("%B")
current_date_format1 = f"{month_name} {current_day}, {current_year}"
current_date_format2 = f"{current_month}/{current_day}/{current_year}"
#create a pedding with spaces b second date
return current_date_format1, current_date_format2.ljust(13)
def main():
st.title('File Editor')
# List of available files
files = ["LOI_ADDRESS_DATE.docx", "LOI_ADDRESS_D.docx", 'file3.txt']
selected_file = st.selectbox('Select a file:', files)
current_date_format1, current_date_format2 = create_date_format()
replacements = ["owner_name",
"property_owner",
"property_address",
"purchase_price",
"property_size_sf",
"price_per_sf",
"brokerage_name"
]
parameter_values = {}
for param in replacements:
parameter_values[param] = st.text_input(f'Enter value for {param}:')
parameter_values["current_date"] = current_date_format2
parameter_values["Month_day_year"] = current_date_format1
# run the file update
modified_doc = replace_text_in_docx(selected_file, parameter_values)
# Save button
if st.button('Save Changes'):
base_name, extension = selected_file.split(".")
new_filename = f"{base_name}_modified{extension}"
# new_filename = "LOI_ADDRESS_DATE_modified.docx"
modified_doc.save(new_filename)
st.success('File saved successfully!')
with open(new_filename, 'rb') as f:
st.download_button('Download Docx', f, file_name='LOI_ADDRESS_DATE_modified.docx')
# Function to replace parameters in file content with provided values
def replace_text_in_docx(docx_file, replacements):
doc = Document(docx_file)
for search_text, replace_text in replacements.items():
for paragraph in doc.paragraphs:
paragraph.text.replace(search_text, replace_text)
if search_text in paragraph.text:
inline = paragraph.runs
for i in range(len(inline)):
if search_text in inline[i].text:
text = inline[i].text.replace(search_text, replace_text)
inline[i].text = text
return doc
# Function to replace parameters in file content with provided values
def replace_parameters(file_contents, parameter_values):
updated_contents = file_contents
for param, value in parameter_values.items():
updated_contents = updated_contents.replace(f'@{param}', value)
return updated_contents
if __name__ == "__main__":
main()