我需要一种方法将
.doc
或 .docx
扩展转换为 .txt
而不安装任何东西。显然我也不想手动打开 Word 来执行此操作。只要它在自动运行。
我认为 Perl 或 VBA 都可以解决这个问题,但我在网上找不到任何东西。
有什么建议吗?
一个简单的仅 Perl 的 docx 解决方案:
使用 Archive::Zip 从
word/document.xml
文件中获取 docx
文件。 (docx 只是一个压缩档案。)使用 XML::LibXML 来解析它。
然后使用XML::LibXSLT将其转换为文本或html格式。搜索网络找到一个不错的 docx2txt.xsl 文件:)
干杯!
J.
请注意,Microsoft Office 应用程序的一个极好的信息来源是对象浏览器。您可以通过
Tools
→ Macro
→ Visual Basic Editor
访问它。进入编辑器后,按 F2 浏览 Microsoft Office 应用程序提供的接口、方法和属性。
这是一个使用 Win32::OLE:
的示例#!/usr/bin/perl
use strict;
use warnings;
use File::Spec::Functions qw( catfile );
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 3;
my $word = get_word();
$word->{Visible} = 0;
my $doc = $word->{Documents}->Open(catfile $ENV{TEMP}, 'test.docx');
$doc->SaveAs(
catfile($ENV{TEMP}, 'test.txt'),
wdFormatTextLineBreaks
);
$doc->Close(0);
sub get_word {
my $word;
eval {
$word = Win32::OLE->GetActiveObject('Word.Application');
};
die "$@\n" if $@;
unless(defined $word) {
$word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit })
or die "Oops, cannot start Word: ",
Win32::OLE->LastError, "\n";
}
return $word;
}
__END__
对于 .doc,我使用 Linux 命令行工具 antiword 取得了一些成功。它可以非常快速地从 .doc 中提取文本,并提供良好的缩进效果。然后您可以将其通过管道传输到 bash 中的文本文件。
对于 .docx,我使用了 OOXML SDK,正如其他一些用户提到的那样。它只是一个 .NET 库,可以更轻松地使用压缩在 OOXML 文件中的 OOXML。如果您只对文本感兴趣,则有很多元数据需要丢弃。其他一些人已经编写了我看到的代码:DocXToText。
Aspose.Words 有一个非常简单的 API,而且我也发现有很好的支持。
还有来自 commandlinefu.com 的 bash 命令,它通过解压缩 .docx 来工作:
unzip -p some.docx word/document.xml | sed -e 's/<[^>]\{1,\}>//g; s/[^[:print:]]\{1,\}//g'
如果您会 Java 或 .NET,我强烈推荐 AsposeWords。无需安装 Word,它就可以在所有主要文本文件类型之间进行转换。
如果您安装了某种风格的unix,则可以使用“字符串”实用程序从文档中查找并提取所有可读字符串。您要查找的文本前后会有一些混乱,但结果是可读的。
请注意,您还可以使用 OpenOffice 在 Windows 和 *nix 平台上执行各种文档、绘图、电子表格等转换。
您可以通过 UNO 以编程方式访问 OpenOffice(类似于 Windows 上的 COM),从存在 UNO 绑定的各种语言,包括通过 OpenOffice::UNO 模块从 Perl 访问。
在 OpenOffice::UNO 页面,您还可以找到一个打开文档的示例 Perl scriptlet,然后您需要做的就是使用
txt
方法将其导出到 document.storeToURL()
- 请参阅 Python 示例 可以轻松适应您的 Perl 需求。
Sinan Ünür 的方法很有效。
但是,我正在转换的文件发生了一些崩溃。
另一种方法是使用
Win32::OLE
和 Win32::Clipboard
:
根据Sigvald Refsu在http://computer-programming-forum.com/53-perl/c44063de8613483b.htm中给出的脚本,我想出了以下脚本。
注意:我选择将 txt 文件保存为与 .docx 文件具有相同基名并保存在同一文件夹中,但这可以轻松更改
###########################################
use strict;
use File::Spec::Functions qw( catfile );
use FindBin '$Bin';
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Word';
use Win32::Clipboard;
my $monitor_word=0; # set 1 to watch MS Word being opened and closed
sub docx2txt {
# Note: the path shall be in the form "C:\dir\ with\ space\file.docx";
my $docx_file=shift;
# MS Word object
my $Word = Win32::OLE->new('Word.Application', 'Quit') or die "Couldn't run Word";
# Monitor what happens in MS Word
$Word->{Visible} = 1 if $monitor_word;
#Open file
my $Doc = $Word->Documents->Open($docx_file);
with ($Doc, ShowRevisions => 0); #Turn of revision marks
# Select the complete document
$Doc->Select();
my $Range = $Word->Selection();
with ($Range, ExtendMode => 1);
$Range->SelectAll();
# Copy selection to clipboard
$Range->Copy();
# Create txt file
my $txt_file=$docx_file;
$txt_file =~ s/\.docx$/.txt/;
open(TextFile,">$txt_file") or die "Error while trying to write in $txt_file (!$)";
printf TextFile ("%s\n", Win32::Clipboard::Get());
close TextFile;
# Empty the Clipboard (to prevent warning about "huge amount of data in clipboard")
Win32::Clipboard::Set("");
# Close Word file without saving
$Doc->Close({SaveChanges => wdDoNotSaveChanges});
# Disconnect OLE
undef $Word;
}
希望可以帮到你。
如果您不想启动 Word(或其他 Office 应用程序),则无法在 VBA 中执行此操作。即使您指的是 VB,您仍然需要启动 Word 的(隐藏)实例来进行处理。
我需要一种无需安装任何东西即可将 .doc 或 .docx 扩展名转换为 .txt 的方法
for I in *.doc?; do mv $I `echo $ | sed 's/\.docx?/\.txt'`; done
开玩笑而已。
您可以对旧版本的Word文档使用antiword,并尝试解析新版本的xml。
使用 docxtemplater,您可以轻松获取单词的全文(仅适用于 docx)。
这是代码(Node.JS)
DocxTemplater=require('docxtemplater');
doc=new DocxTemplater().loadFromFile("input.docx");
result=doc.getFullText();
这只是三行代码,不依赖于任何单词实例(都是纯JS)