Skip to content

Instantly share code, notes, and snippets.

@bjverde
Last active January 29, 2025 15:17
Show Gist options
  • Select an option

  • Save bjverde/a6e822c91b0826ce05930f0f9aaec61c to your computer and use it in GitHub Desktop.

Select an option

Save bjverde/a6e822c91b0826ce05930f0f9aaec61c to your computer and use it in GitHub Desktop.
Convert multiple files to ANSI encoding with Notepad++
# 2017
# Reinaldo Barreto Jr
#
# This file will search all files and folders within a given directory, and use Notepad++ to convert their encoding to ANSI.
# Based on the script of ConvertUTF-8.py de Scott McCutchen
#
# This file must be run using the PythonScript plugin from within Notepad++, which is available through the Notepad++ Plugin Manager
#
# You must have Python 2.7 installed
#
# Additionally, this script can only exist and be run from within the Notepad++ user's working directory, the default of which is here:
# Note that selecting "New Script" from within the PythonScript plugin will automatically default to this save location
#
# .. USER DIRECTORY\AppData\Roaming\Notepad++\plugins\Config\PythonScript\scripts
#
# WARNING !! This script will only work if Notepad ++ is in English. To use in your native language use the terms of the translation
#
# PythonScript Site - http://npppythonscript.sourceforge.net/download.shtml
# PythonScript Documentation - http://npppythonscript.sourceforge.net/docs/latest/
# PythonScript Forum - https://sourceforge.net/p/npppythonscript/discussion/
#
#ConvertANSI.py
import os;
import sys;
from Npp import notepad
filePathSrc="D:\\wamp\www\\ansi\\formDin\\base" # Path to the folder with files to convert
console.write(filePathSrc + "\r\n")
for root, dirs, files in os.walk(filePathSrc):
for fn in files:
if fn[-4:] == '.php' or fn[-4:] == '.inc' or fn[-4:] == '.css' or fn[-4:] == '.js' or fn[-4:] == '.htm' or fn[-5:] == '.html': # Specify file types, taking care to change the fn[number] to correspond to length of the file's extension including the .
notepad.open(root + "\\" + fn)
console.write(root + "\\" + fn + "\r\n")
notepad.runMenuCommand("Encoding", "Convert to ANSI")
notepad.save()
notepad.close()
@PedroAmaral89
Copy link

Hello, im trying to convert multiple files UFT-8 to Windows-1252. Do u know how do it? thx my friend

@bjverde
Copy link
Author

bjverde commented May 19, 2024

@PedroAmaral89 see example below

import os
import sys
from Npp import notepad, console

# Path to the folder with files to convert
filePathSrc = "D:\\wamp\\www\\ansi\\formDin\\base"

console.write(filePathSrc + "\r\n")

def convert_to_windows1252(file_path):
    # Lê o conteúdo do arquivo em UTF-8
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()

    # Salva o conteúdo em Windows-1252
    with open(file_path, 'w', encoding='windows-1252') as file:
        file.write(content)

for root, dirs, files in os.walk(filePathSrc):
    for fn in files:
        if fn.endswith(('.php', '.inc', '.css', '.js', '.htm', '.html')):  # Specify file types
            file_path = os.path.join(root, fn)
            console.write(file_path + "\r\n")
            convert_to_windows1252(file_path)
            notepad.open(file_path)
            notepad.save()
            notepad.close()

console.write("Conversão concluída!\r\n")

@PedroAmaral89
Copy link

Very thanks. But I think im going wrong on something. Doesn't work here. Trying in .xml files, running script on notepad++ nothing happens. The previous script run but i dont know how input windows-1252 there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment