如何使用perl找出段落的对齐方式



有没有一种方法可以找出MS Word文档中段落的对齐方式。谁能帮我?

使用 OLE,看起来您可以通过具有 Alignment 属性的 ParagraphFormat2 对象获取对齐(或对齐)。 下面是 OLE 文档中的示例:

ActivePresentation.Slides(1).Shapes(2).TextFrame2.TextRange2.ParagraphFormat2.Alignment

您可以在此处阅读有关此对象的更多信息。

要提供一个 Perl 示例,请看这个示例:

use strict;
use warnings;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Word';
use Win32::OLE::Variant;
my $word = Win32::OLE->GetActiveObject('Word.Application')
  || Win32::OLE->new( 'Word.Application', 'Quit' );
$word->{Visible} = 1;
my $doc = $word->{Documents}->Open('<full path to file>');
print $doc->Paragraphs(1)->{Alignment} . "n";
$doc->Close();

您至少需要在安装了 Word 的计算机上安装 Win32::OLE Microsoft库。 在编写 Perl 应用程序以使用 OLE 时,任何属于 OLE 对象的东西都是方法调用,任何属于 OLE 成员的东西都是哈希引用。

当您打开文件时,您需要提供文件的完整路径,即"C:\\folder\\doc.docx"。 将传递到Paragraphs的所需段落的数字更改为(在 OLE 数组中,从 1 开始。

Alignment键将返回一个 int,对应于一个 WdParagraphAlignment Enumeration 。 我能够对此进行测试;0 => 左,1 =>居中,2 =>右,3 =>对齐。

最新更新