Ricky Hao

Notepad++ 使用 Python 脚本批量转换文件格式

首先,我们需要将Notepad++语言改为英文。
接下来,下载Python Script插件的压缩包(我选的是Full版本)。
最后,把压缩包解压到Notepad++的程序根目录
最终的文件树应该是这样:

Notepad++            (your main Notepad++ directory, probably under "C:\Program Files")
 +
 |-- python26.dll     (this is important. This needs to go in the main program directory of Notepad++,     next to notepad++.exe)
 |                    (unless you have a Python installation already, in which case it's probably in     C:\windows already)
 +-- plugins
           \
           |-- PythonScript.dll
           |
           |-- PythonScript
           |   \
           |   |-- lib
           |   |    \
           |   |     |-- (*.py)   lots of *.py files and subdirectories
           |   |
           |   |-- scripts
           |             \
           |             |-- (machine-level scripts)
           |
           |
           |-- doc
           |    \
           |     |-- PythonScript
           |             \
           |             |-- PythonScript.chm         (optional, if it's not there context-sensitive help will use the web)
           |
           |
           |
           |
           |
           |-- Config     (this config directory can also be in %APPDATA%\Notepad++\plugins\config\)
                         |
                         \-- PythonScript
                                         \
                                         |-- scripts
                                                   \
                                                   |-- (user level scripts go here)    

现在,打开Notepad++->Plugins->Python Script->Show Console
若是能成功弹出命令行窗口,那便是安装成功了。

接下来,让我们来编写脚本(这里只能使用Python2.7的语法)

#coding=utf-8
import os
import sys

os.chdir(r"D:\test")
for path in os.listdir(r"D:\test"):
        print(path)
        notepad.open(path)
        notepad.runMenuCommand("Encoding","Convert to UTF-8")
        notepad.save()
        notepad.close()

这代码也是非常简洁明了的,嗯,这样就可以批量将test下的所有文件转换为UTF-8格式了。

点赞

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据