File size: 3,332 Bytes
84fd222
b2318db
b9379c6
 
 
b2318db
84fd222
 
 
 
 
 
 
 
 
 
 
b9379c6
 
 
 
 
 
11c3871
b9379c6
 
 
 
 
84fd222
 
 
 
f375d70
84fd222
 
b9379c6
 
 
b27bb68
 
 
 
 
 
b9379c6
c3115cf
84fd222
b9379c6
84fd222
b9379c6
 
 
 
01dea6b
b9379c6
84fd222
 
4e55ce2
c03a40b
 
7cacac8
b27bb68
18025e5
b27bb68
10b940a
f25a134
84fd222
b9379c6
 
 
 
 
 
 
 
 
 
 
01dea6b
61e81ce
 
 
 
 
 
 
96210f9
0966b09
84fd222
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

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()