Downloading a Word document returned by function(s)

I am designing a Streamlit app that allows to examine a Word document for Arial or Abadi font.
The app allows to upload a Word document via the file uploader. There are two checkboxes that allow to select a function to execute, one that checks for Arial font and another that checks for Abadi font. The function(s) are executed by clicking the Run button.

If the app finds text written in Arial or Abadi it will underline such text. To achieve this I use the python-docx library (version 0.8.11). These edits are saved as separate documents (one for each function, depending on which is executed).

The problem that I am facing is downloading the edited documents. I have tired to return the edited documents from their respective functions, but the download_button widget does not accept this method due to the error: NameError: name ‘edited_doc1’ is not defined.

Another problem is that in some instances (in which both functions are executed and the file is edited by both functions) multiple documents will need to be downloaded. I do not understand how this can be accomplished. Any form of help would be appreciated.

The code is shown here:

# import modules
from docx import Document  # for accessing the document by python-docx
import streamlit as st  # for web app


# Arial check
def func1():
    global edited_doc1
    # access Word document
    my_file = Document(uploaded_file)
    FONT = 'Arial'  # state the specified font
    for paragraphs in my_file.paragraphs:
        if 'Normal' == paragraphs.style.name:
            for run in paragraphs.runs:
                if run.font.name == FONT:
                    # underline text written in Arial font
                    run.underline = True
                    # save changes to file
                    edited_doc1 = my_file.save("Edited_Document1.docx")
    return edited_doc1


# Abadi check
def func2():
    global edited_doc2
    # access Word document
    my_file = Document(uploaded_file)
    FONT = 'Abadi'  # state the specified font
    for paragraphs in my_file.paragraphs:
        if 'Normal' == paragraphs.style.name:
            for run in paragraphs.runs:
                if run.font.name == FONT:
                    # underline text written in Abadi font
                    run.underline = True
                    # save changes to file
                    edited_doc2 = my_file.save("Edited_Document2.docx")
    return edited_doc2


# function to run selected programs(s)
def run_program():
    if cb1: func1()
    if cb2: func2()


# configure sidebar text and widgets
uploaded_file = st.sidebar.file_uploader("Select Word document", type='.docx', key='a')
cb1 = st.sidebar.checkbox('Arial check', key='b')
cb2 = st.sidebar.checkbox('Abadi check', key='c')
run_btn = st.sidebar.button('Run', on_click=run_program, key='d')
dwnld_btn = st.sidebar.download_button(label='Download edited document', data=[edited_doc1, edited_doc2], key='e')

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.