Page 1 of 1

Notepad++ and Python

Posted: Sat Jul 18, 2020 9:49 am
by Wim Gielis
Hi there,

As a good number of us are using Notepad++ for day to day operations (including rules and TI writing ;-)), I wanted to highlight that Notepad++ has a Python script plugin: http://npppythonscript.sourceforge.net/download.shtml. You can also install it from the "Plugins manager".

This allows us to, for example, count all non-empty lines in all opened files (polish the code a bit):

Code: Select all

from Npp import *
import re
import ntpath

all_files_line_count = 0
s = "The total line count of all open files is:" + "\r\n"
tuple_list = notepad.getFiles()
for tuple in tuple_list:
    filename = tuple[0]
    if filename == "new  0": continue
    notepad.activateFile(filename)
    # parse the active editor's text
    this_files_line_count = 0
    Lines = re.split('\r\n|\r|\n|', editor.getText())
    for line in Lines:
        line = line[0:].strip()
        if line != "":
            this_files_line_count += 1
    
    all_files_line_count += this_files_line_count
    s = s + "\r\n" + ntpath.basename(filename) + "\t\t\t" + str(this_files_line_count)
notepad.messageBox(s + "\r\n\r\n" + "Total:" + "\t\t\t" + str(all_files_line_count), "", MESSAGEBOXFLAGS.OK)
Happy coding !

Wim