Get paragraphs from a Word document using Perl

This Perl script gets all the paragraphs from a Microsoft Word document.

use warnings;
use strict;
use Win32::OLE;
my $document_name = 'C://Users/ben/test.docx';
my $word = Win32::OLE->GetActiveObject('Word.Application')
    || Win32::OLE->new('Word.Application','Quit')
    or die Win32::OLE->LastError();
my $document = $word->Documents->Open($document_name)
    or die Win32::OLE->LastError();
my $paragraphs = $document->Paragraphs ();
my $n_paragraphs = $paragraphs->Count ();
for my $p (1..$n_paragraphs) {
    my $paragraph = $paragraphs->Item ($p);
    print "Paragraph $p:\n", $paragraph->Range->Text (), "\n";

Web links

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer