提供三种办法:

一、用 VBA 来实现(适用于 Windows):

(1)首先打开顶部菜单栏的「开发工具」选项卡,方式如下:

(2)打开任意一个空白的 Word 文档,点击顶部菜单栏上的「开发工具」→「Visual Basic」,弹出一个新窗口(Microsoft Visual Basic for Application);在新窗口的左侧空白处右键,选择「插入」→「模块」,然后在右侧新出现的编辑器中输入下图中的 VBA 代码,然后点击顶部菜单栏中的运行按钮(三角形),运行该程序。

(3)运行之后,按照下图提示,选择名称为「ConvertDocuments」的宏运行,然后在弹出的“选择文件夹”的框中选择你想要处理的 Word 文档所在的文件夹,点击「确认」即可。

(4)运行结束后,在上一步选择的文件夹内就会生成每一个 .doc 文档对应的 txt 文件。

上面第(2)步中用的代码如下(修改自 https://answers.microsoft.com/en-us/msoffice/forum/all/batch-program-to-convert-doc-to-txt/ede736d1-3304-43bc-8739-060df1b21247):

需要注意的是:

1. 如果你需要处理的 Word 文档后缀为 .docx 而不是 .doc,那么需要将代码中的所有 .doc(共两处)手动替换成 .docx

2. 如果你希望修改生成的 txt 文件的编码格式(默认是 UTF8),那么需要将代码中的 msoEncodingUTF8 手动替换为你想要的编码对应的名称,如 msoEncodingSimplifiedChineseGBK 和 msoEncodingSimplifiedChineseGB18030 等。相关信息可以在官方文档中查询。

Sub ConvertDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc")
While strFile <> ""
If strFolder & "\" & strFile <> strDocNm Then
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
.SaveAs FileName:=strFolder & "\" & Split(strFile, ".doc")(0) & ".txt", FileFormat:=wdFormatText, AddToRecentFiles:=False, Encoding:=msoEncodingUTF8
.Close SaveChanges:=True
End With
End If
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "请选择包含要处理的 Word 文档的文件夹:", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

更新代码(支持递归处理所选取文件夹下的所有子文件夹中的文档):

Sub ConvertDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFiles As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
Debug.Print strFolder
If strFolder = "" Then Exit Sub
strFiles = LoopThroughFiles(strFolder, ".doc", True)

Dim iFiles() As String
iFiles() = Split(strFiles, vbTab)

Dim i As Long
For i = LBound(iFiles) To UBound(iFiles)
If iFiles(i) <> "" And iFiles(i) <> strDocNm Then
Set wdDoc = Documents.Open(FileName:=iFiles(i), AddToRecentFiles:=False, Visible:=False)
With wdDoc
.SaveAs FileName:=Split(iFiles(i), ".doc")(0) & ".txt", FileFormat:=wdFormatText, AddToRecentFiles:=False, Encoding:=msoEncodingUTF8
.Close SaveChanges:=True
End With
End If
Next i
Set wdDoc = Nothing
Application.ScreenUpdating = True
MsgBox "已转换" & UBound(iFiles) & "个文档"
End Sub

Private Function LoopThroughFiles(inputDirectory As String, filenameCriteria As String, doTraverse As Boolean) As String
Dim tmpOut As String
Dim StrFile As String

If doTraverse = True Then
Dim allFolders As String
Dim iFolders() As String
allFolders = TraverseDir(inputDirectory & "\", 1, 100)
iFolders() = Split(allFolders, vbTab)

tmpOut = LoopThroughFiles(inputDirectory, filenameCriteria, False)
Dim j As Long
For j = LBound(iFolders) To UBound(iFolders)
If iFolders(j) <> "" Then
StrFile = LoopThroughFiles(iFolders(j), filenameCriteria, False)
tmpOut = tmpOut & vbTab & StrFile
End If
Next j
LoopThroughFiles = tmpOut
Else
'https://stackoverflow.com/a/45749626/4650297
StrFile = Dir(inputDirectory & "\*" & filenameCriteria)
Do While Len(StrFile) > 0
tmpOut = tmpOut & vbTab & inputDirectory & "\" & StrFile
StrFile = Dir()
Loop
LoopThroughFiles = tmpOut
End If
End Function

Private Function TraverseDir(path As String, depth As Long, maxDepth As Long) As String
'https://analystcave.com/vba-dir-function-how-to-traverse-directories/#Traversing_directories
If depth > maxDepth Then
TraverseDir = ""
Exit Function
End If
Dim currentPath As String, directory As Variant
Dim dirCollection As Collection
Set dirCollection = New Collection
Dim dirString As String

currentPath = Dir(path, vbDirectory)

'Explore current directory
Do Until currentPath = vbNullString
' Debug.Print currentPath
If Left(currentPath, 1) <> "." And (GetAttr(path & currentPath) And vbDirectory) = vbDirectory Then
dirString = dirString & vbTab & path & currentPath
dirCollection.Add currentPath
End If
currentPath = Dir()
Loop

TraverseDir = dirString
'Explore subsequent directories
For Each directory In dirCollection
TraverseDir = TraverseDir & vbTab & TraverseDir(path & directory & "\", depth + 1, maxDepth)
Next directory
End Function

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "请选择包含要处理的 Word 文档的文件夹:", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

二、用 textutil 命令来实现(适用于 Mac OS)

textutil 是 Mac OS 上的标准命令,只需要打开 Terminal app,按照以下方式执行命令即可在目标文件夹下批量生成每个 Word 文档对应的 txt 文件:

textutil -convert txt -encoding utf-8 ~/Desktop/test\ docs/*

其中 ~/Desktop/test\ docs/* 是我测试用的文件路径,它匹配了 test docs 文件夹下的所有文档。

三、用开源软件 LibreOffice 的命令行来实现(可跨平台使用)

首先下载 LibreOffice,然后在 “应用程序 / Applications” 目录下找到 LibreOffice.app,在它上面右键 →「显示包内容 / Show Package Contents」,依次打开 app 的「Contents」→「MacOS」目录,然后选择 soffice 文件,点击右键并按住 option 键,选择「拷贝 “soffice” 为路径 / Copy "soffice" as Pathname」。

然后在 Terminal 中运行以下命令即可(其中 ~/Desktop/test\ docs/* 是我测试用的文件路径,它匹配了 test docs 文件夹下的所有文档):

/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to txt:Text ~/Desktop/test\ docs/*
运行之后默认会在桌面上生成对应的 txt 文档,效果如下:

声明:本站所收录作品、热点评论、图片等信息部分来源互联网,目的只是为了系统归纳学习和传递资讯。内容不代表本网站的观点和立场。请读者仅作参考,并请自行核实相关内容。本站所有图文由于未联系到知识产权人或未发现有关知识产权的登记,所有作品版权归原创作者所有,根据《信息网络传播权保护条例》,如不慎侵犯了你的权益,请联系我们告知,我们将做删除处理!