Search code examples
pythonpython-2.7automationpywinauto

How to save multiple files with incrementing names using pywinauto


I'm trying to write a simple script that will increment the text inside of a file by +1 and then save the file while incrementing the file name by +1.

So basically:

This is text 1

Saved as: File1.txt

This is text 2

Saved as: File2.txt

And so on. I've gotten my script to do this with the notepad program however I can't figure out a way to do this without having to open notepad for every file. I've played around with trying to connect to the process id and such but I'm only about a month into programming so I'm having a terrible time with the concept.

#!/usr/bin/env python

from pywinauto.application import Application
b = int(raw_input("Where do you want to start? >"))
a = int(raw_input("How Many Labels do you need? >"))

def make_labels(a, b):

    app = Application().start("notepad.exe")
    app.UntitledNotepad.edit1.SetText("This is me typing %r" % b)
    app.UntitledNotepad.MenuSelect("File -> SaveAs")
    #app.SaveAs.ComboBox5.Select("UTF-8")
    app.SaveAs.edit1.SetText("Test_File%r.txt" % b)
    app.SaveAs.Save.Click()
    app.UntitledNotepad.TypeKeys("%FX")
    b = b + 1
    return b



while b < a:
    b = make_labels(a, b)

Any help at all figuring out how to get this to work only using one instance of notepad would be greatly appreciated.

Update:

Okay so I've made a little progress I think. I didn't know that you could just identify it by the program name so I changed that and now it uses the same window. However my new issue is that it will get to the point where it increments the contents by +1 a second time however it will fail to open the save option afterwards:

#!/usr/bin/env python

from pywinauto.application import Application
import time

b = int(raw_input("Where do you want to start? >"))
a = int(raw_input("How Many Labels do you need? >"))
app = Application().start("notepad.exe")

def make_labels(b, app): 

    #app = Application().start("notepad.exe")
    app.Notepad.edit1.SetText("This is me typing %r" % b)
    app.Notepad.MenuSelect("File -> SaveAs")
    app.SaveAs.edit1.SetText("Test_File%r.txt" % b)
    app.SaveAs.Save.Click()

    #app.Notepad.TypeKeys("%FX")
    b = b + 1
    return b



while b < a:
     b = make_labels(b, app)

Solution

  • Alright!

    So I figured out an alternate way of solving my problem. Instead of using the menu select option as if a person was clicking it with their mouse I instead tried an all Keyboard Shortcut way and it worked:

    #!/usr/bin/env python
    
    from pywinauto.application import Application
    import time
    
    b = int(raw_input("Where do you want to start? >"))
    a = int(raw_input("How Many Labels do you need? >"))
    app = Application().start("notepad.exe")
    
    def make_labels(b, app): 
    
        #app = Application().start("notepad.exe")
        app.Notepad.edit1.SetText("This is me typing %r" % b)
        app.Notepad.TypeKeys("^S")
        app.SaveAs.edit1.SetText("Test_File%r.txt" % b)
        app.SaveAs.Save.Click()
    
        #app.Notepad.TypeKeys("%FX")
        b = b + 1
        return b
    
    
    
    while b < a:
        b = make_labels(b, app)
        app.Notepad.TypeKeys("^N")
    

    This uses the keyboard inputs to save the file and then open a new file (under the same application instance) and repeat the process.

    I hope this can be helpful to someone else in the future.