Importing Data from Docx File

Back again! Hit another snag that I am hoping you guys can help with.

What I’m trying to do:
I’m trying to allow the user to upload a Word Document and then parse that data to fill in a form I created.

What I’ve tried and what’s not working:
I have a file loader on the form and I wrote the code to parse the file. This was tested outside of the Anvil environment to ensure it works. But when I try in Anvil I get the following error: FileNotFoundError: [Errno 2] No such file or directory: ‘/tmp/f0mzpzips0yf9dmcnh4atc859yms7nx3’

Code Sample:

@handle("import_data", "click")
  def import_data_click(self, **event_args):
    anvil.server.call('import_sr_data', self.upload.file)
#  this is the code in the form
# this is the code in my server module
@anvil.server.callable
def import_sr_data(file):
  def get_all_text_elements(docx_path):
    with zipfile.ZipFile(docx_path) as zf:
      xml_content = zf.read('word/document.xml')
    tree = ET.fromstring(xml_content)
    ns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
    text_elements = tree.findall('.//w:t', ns)
    return [el.text for el in text_elements if el.text]

  detective_contact_count = 0
  detective_email_count = 0
  dag_contact_count = 0
  dag_email_count = 0

  new_file = None
  def write_media_to_file(media_object):
    with anvil.media.TempFile(media_object) as file_name:
      # Now there is a file in the filesystem containing the contents of media_object.
      # The file_name variable is a string of its full path.
      
      return file_name

  new_file = write_media_to_file(file)
      
  text = get_all_text_elements(new_file)

  for index, value in enumerate(text):
    if value == 'Date:':
      item = index + 1
      print(f"Submission Date: {text[item]}")
    elif value == 'Case Name:':
      item = index + 1
      print(f"Case: {text[item]}")
    elif value == 'Case Number:':
      item = index + 1
      print(f"Case Number: {text[item]}")
    elif value == 'Crime:':
      item = index + 1
      print(f"Crime: {text[item]}")
    elif value == 'Search Location:':
      item = index + 1
      print(f"Search Location: {text[item]}")
    elif value == 'Date of Search/Seizure:':
      item = index + 1
      dos = ''
      count = item + 6
      while item < count:
        dos += text[item]
        item += 1
      print(f"Date of Search/Seizure: {dos}")
    elif value == 'Legal Authority:':
      item = index + 1
      print(f"Legal Authority: {text[item]}")
    elif value == 'DETECTIVE':
      item = index + 3
      print(f"Detective: {text[item]}")
    elif value == 'Contact ' and detective_contact_count < 1:
      item = index + 3
      print(f"Contact: {text[item]}")
      detective_contact_count += 1
    elif value == 'Email:' and detective_email_count < 1:
      item = index + 1
      print(f"Detective Email: {text[item]}")
      detective_email_count += 1
    elif value == 'DEPUTY ATTORNEY GENERAL':
      item = index + 3
      print(f"DAG: {text[item]}")
    elif value == 'Contact ' and dag_contact_count < 1:
      item = index + 3
      print(f"DAG Phone Number: {text[item]}")
      dag_contact_count += 1
    elif value == 'Email ' and dag_email_count < 1:
      item = index + 1
      print(f"DAG Email: {text[item]}")
      dag_email_count += 1
    elif value == 'SUSPECT':
      item = index + 2
      print(f"Suspect: {text[item]}{text[item+1]}")
    elif value == 'DOB:':
      item = index + 1
      print(f"DOB: {text[item]}")
    elif value == 'Street:':
      item = index + 1
      print(f"Street: {text[item]}")
    elif value == 'City:':
      item = index + 1
      print(f"City: {text[item]}")
    elif value == 'State:':
      item = index + 1
      print(f"State: {text[item]}")
    elif value == 'Zip:':
      item = index + 1
      print(f"Zip: {text[item]}")

Any help would be greatly appreciated!!

Take a look at the Media Object to temporary file documentation.

The file is written to the filesystem as soon as the with block begins. The file will be deleted when the with block exits.

So, you got the file path to a file that did exist, but then was discarded as soon as the file path was returned. You will need to work with the file within the with block.

3 Likes

That did the trick! Missed the part in the documentation that the file disappears outside of the with block. Thank you again, you are the best!

2 Likes