What I’m trying to do:
Hello I am working on a code that will upload multiple files in Anvil and join them together and then show the joined text in a section called context if I click on a button.
What I’ve tried and what’s not working:
The code is already working in my jupyter notebook and I have tried implementing it in Anvil, however it seems that the files are not being stored. Getting error:
`AttributeError: 'FileMedia' object has no attribute 'path'`
* `at Form1, line 17`
Code Sample:
from ._anvil_designer import Form1Template
from anvil import *
import anvil.server
class Form1(Form1Template):
def __init__(self, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# Any code you write here will run before the form opens.
def button_1_click(self, **event_args):
# Clear the context area component before displaying the new text
self.context.text = ""
# Get the list of uploaded file paths from the Upload component
file_paths = [f.path for f in self.file_uploader.files]
# Initialize an empty list to store the text from all files
text_list = []
# Loop through each file path and add its text to the list
for file_path in file_paths:
if file_path.endswith(".docx"):
doc = docx.Document(file_path)
for para in doc.paragraphs:
text_list.append(para.text)
# Join all the text together into a single string
text = " ".join(text_list)
# Display the concatenated text in the TextArea component
self.context.text = text
Clone link:
share a copy of your app