您的当前位置:首页正文

Notepad++批量修改文件编码格式

来源:化拓教育网
Notepad++批量修改⽂件编码格式

⼀、安装notepad++ 插件管理器(PluginManager)

解压⾄notepad++ 的plugins⽬录,重启即可

ps: 7.6x以上版本,不能直接复制到notepad⽬录下。需要在plugins⽬录下,新建⽴PluginManager⽬录。然后把PluginManager.dll拷贝到此⽬录下,才能够显⽰出来效果

⼆、安装Python Script 插件

1. Run Notepad++ and then open menu Plugins->Plugin Manager->Show Plugin Manager  运⾏notepad++ 打开插件--》插件管理器--》显⽰插件管理

  2、Install Python Script. When plugin is installed, restart the application.  安装Python Script 等待安装完毕,重启应⽤

三、新建python脚本,进⾏格式转换

1. Choose menu Plugins->Python Script->New script.2. 选择插件--》python 脚本--》新建脚本  

1. Choose its name, and then past the following code:

  选择当前⽬录,新建名称为“convertToUTF8.py” 的脚本,脚本内容如下

import osimport sys

sys.stdout = console

filePathSrc=\"E:\\\\new\\\\\" # Path to the folder with files to convertfor root, dirs, files in os.walk(filePathSrc): for fn in files: print fn[-5:]

if fn[-5:]=='.json':# Specify type of the files print fn[-5:]

notepad.open(root + \"\\\\\" + fn)

notepad.runMenuCommand(\"Encoding\",\"Convert to UTF-8\") notepad.save() notepad.close()

重启后则可以看到相应的脚本名称,并可以运⾏脚本

  如果要转化为ANSI 就把下⾯UTF-8改为ANSI既可,说明⼀下下⾯的fn[-5:],指寻找后⾯5个字符匹配的路径后缀为.html ,如果你要匹

配.cpp ,则应该是fn[-4:],下⾯包含了.html .cpp⼀起修改。转换成不同的编码格式,只需修改 Convert to UTF-8 为下⾯菜单的红⾊框⾥⾯对应项即可。我的是notepad++7.5.8的,不同版本可能稍有不同。设置为跟⾃⼰版本⼀致即可。

import osimport sys

sys.stdout = console

filePathSrc=\"E:\\\\new\\\\\" # Path to the folder with files to convertfor root, dirs, files in os.walk(filePathSrc): for fn in files: print fn[-5:]

if fn[-5:]== '.html' or fn[-4:]=='.cpp':# Specify type of the files print fn[-5:]

notepad.open(root + \"\\\\\" + fn)

notepad.runMenuCommand(\"Encoding\",\"Convert to UTF-8\") notepad.save() notepad.close()

运⾏结束后就会返现 编码格式批量转换成功

四、在这⾥需要说明的以下注意⼏点

1. notepad ++ 必须是在 英⽂状态下上述 ⽅法才有效2. filePathSrc 路径中不能包含中⽂

3. python不⽀持tab 和空格混⽤ 缩进,所以最好是打开notepad++ 的 View--》显⽰空格与制表符 如上⾯图所⽰

因篇幅问题不能全部显示,请点此查看更多更全内容