繁体   English   中英

将Word doc或docx文件转换为文本文件?

[英]Convert Word doc or docx files into text files?

我需要一种方法将.doc.docx扩展名转换为.txt而不安装任何东西。 我也不想手动打开Word来显然这样做。 只要它在auto上运行。

我在想Perl或VBA可以做到这一点,但我也无法在网上找到任何东西。

有什么建议?

一个简单的Perl唯一解决方案:

  1. 使用Archive :: Zipdocx文件中获取word/document.xml文件。 (docx只是一个压缩档案。)

  2. 使用XML :: LibXML来解析它。

  3. 然后使用XML :: LibXSLT将其转换为文本或html格式。 在网上找到一个不错的docx2txt.xsl文件:)

干杯!

J.

请注意,Microsoft Office应用程序的一个极好的信息来源是对象浏览器 您可以通过ToolsMacroVisual 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风格,你可以使用'strings'实用程序从文档中查找并提取所有可读字符串。 在您要查找的文本之前和之后会有一些混乱,但结果将是可读的。

请注意,您还可以使用OpenOffice在Windows和* nix平台上执行各种文档,绘图,spreadhseet等转换。

您可以通过UNO以编程方式(以类似于Windows上的COM的方式)从存在UNO绑定的各种语言(包括来自Perl的OpenOffice :: UNO模块)访问OpenOffice

OpenOffice :: UNO页面上,您还将找到一个打开文档的示例Perl scriptlet,然后您需要做的就是使用document.storeToURL()方法将其导出到txt - 请参阅Python示例 ,该示例很容易适应您的Perl需求。

.doc使用WordprocessingML.docx的XML格式可以解析其XML以检索文档的实际文本。 您必须阅读他们的规范以确定哪些标签包含可读文本。

SinanÜnür的方法效果很好。
但是,我正在改变我正在改造的文件。

另一种方法是使用Win32 :: OLE和Win32 :: Clipboard:

  • 打开Word文档
  • 选择所有文本
  • 在剪贴板中复制
  • 在txt文件中打印剪贴板的内容
  • 清空剪贴板并关闭Word文档

基于Sigvald Refsu在http://computer-programming-forum.com/53-perl/c44063de8613483b.htm中提供的脚本,我想出了以下脚本。

注意:我选择使用与.docx文件相同的基本名称保存txt文件并保存在同一文件夹中,但这可以很容易地更改

########################################### 
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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM