# File creation and editing in Ignition

**URL:** <https://forum.inductiveautomation.com/t/file-creation-and-editing-in-ignition/92816>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 4, 2024, 8:21pm UTC](https://forum.inductiveautomation.com/t/file-creation-and-editing-in-ignition/92816 "2024-07-04T20:21:52Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![simeonw](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@simeonw](https://forum.inductiveautomation.com/u/simeonw)\
**Post date:** [July 4, 2024, 9:20pm UTC](https://forum.inductiveautomation.com/t/file-creation-and-editing-in-ignition/92816/2 "2024-07-04T21:20:03Z")

</div>

Ignition has system functions for reading and writing files, which will work well for plain text files:

> **[system.file | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-file)**
>
> File Functions

You should be able to use the readFileAsString and writeFile functions.

Word documents require a bit more work. There is a good example here showing how to use Apache POI within Ignition (no external library required):

> [@DOCX - Apache POI](https://forum.inductiveautomation.com/t/docx-apache-poi/71426):
>
> Been looking for a way to read/edit Word (docx) files and ended up reading through a few posts here: Tried reading through a bit of the Apache POI documentation for working with Word and tried to import org.apache.poi.xwpf and received ImportError: No module named xwpf I checked with import org.apache.poi.ss from the second forum post and that seems to work no problem. Can anyone steer me in the right direction to get started with reading docx files?

I've modified it slightly to read a file, modify the ISBN and save it again:

```python
import re
from java.io import FileOutputStream, FileInputStream, IOException
from org.apache.poi.xwpf.usermodel import XWPFDocument

filePath = "someFile.docx" #Modify this with your Word document file path

# New ISBN number to replace with
newISBN = "AAAAAAA"

# Regular expression pattern to match the ISBN number
pattern = re.compile(r'ISBN:\s*([a-zA-Z0-9]+)')

# Initialize file input and output streams outside the try block
fis = None
fos = None

try:
    # Load the Word document
    fis = FileInputStream(filePath)
    doc = XWPFDocument(fis)

    # Iterate through paragraphs
    for paragraph in doc.getParagraphs():
        
        # Concatenate text from all runs within the paragraph. Sometimes the search string isn't always in a single run.
        text = ""
        for run in paragraph.getRuns():
            text += run.getText(0)
		
        if text:
			
			
			# Use regex to find matches
			matches = pattern.findall(text)
			for match in matches:
				
			    # Replace only the ISBN number with the new ISBN
			    modified_text = text.replace(match, newISBN)
			
			# Clear existing runs in the paragraph
			for i in reversed(range(paragraph.getRuns().size())):
			    paragraph.removeRun(i)
			
			# Insert the modified text into a new run in the paragraph
			run = paragraph.createRun()
			run.setText(modified_text)

    # Save the modified document
    fos = FileOutputStream(location)
    doc.write(fos)

    print("ISBN replacement completed successfully.")

except IOException as e:
    e.printStackTrace()

finally:
    # Close streams in finally block to ensure they are always closed, even if an exception is thrown somewhere
    try:
        if fos:
            fos.close()
        if fis:
            fis.close()
    except IOException as e:
        e.printStackTrace()

```

Here is a link to the Apache POI documentation:  
[https://poi.apache.org/apidocs/dev/org/apache/poi/xwpf/usermodel/XWPFDocument.html](https://poi.apache.org/apidocs/dev/org/apache/poi/xwpf/usermodel/XWPFDocument.html)

---

_[View the full topic](https://forum.inductiveautomation.com/t/file-creation-and-editing-in-ignition/92816)._
