Skip to content

Instantly share code, notes, and snippets.

@chankeypathak
Last active November 18, 2016 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chankeypathak/a90a0745e2f8bd1ca117 to your computer and use it in GitHub Desktop.
Save chankeypathak/a90a0745e2f8bd1ca117 to your computer and use it in GitHub Desktop.
Win32::OLE examples
#!/usr/bin/perl
#Author: Chankey Pathak
#Date: 29 Sept 2015
#TODO: process files of entire directory
use strict;
use warnings;
use Data::Dump qw(dump);
use Getopt::Long;
use Win32::OLE qw(valof);
use PDF::API2;
#Basic config part
my $APPNAME = "UpdateContent.pl";
my %opts;
Getopt::Long::GetOptions( \%opts,
'filepath=s',
'content=s',
'help'
) or exit(1);
#Validation
unless (defined $opts{filepath}) { printHelp(); exit 0; }
unless (-e $opts{filepath}) { print "$opts{filepath} doesn't exist"; exit;}
my $ext = $1 if $opts{filepath} =~ /.*(\..*)$/;
exit unless defined $ext;
my $time = localtime;
if (defined $opts{content} && $opts{content} eq 'timestamp' ) {
print "Appending timestamp $time to file\n";
}
else{
print "Appending $opts{content} to file";
}
if($ext =~ /\.doc(x)?/){
edit_word_file();
}
elsif($ext =~ /\.xls(x)?/){
edit_excel_file();
}
elsif($ext eq '.rtf'){
edit_rtf_file();
}
elsif($ext eq '.pdf'){
edit_pdf_file();
}
else{
print "Unable to process fileformat $ext\n";
exit;
}
#Helper methods
sub edit_word_file{
my $file= $opts{filepath};
my $word = Win32::OLE->new('Word.Application', 'Quit') or die "Could not find Word.\n";
my $doc = $word->Documents->Open($file) or die $!;
my $content = valof $doc->{Content};
if ($opts{content} eq 'timestamp'){
$content = $content.$time;
}
else{
$content = $content.$opts{content};
}
$doc->{Content} = $content;
$doc->Save();
$doc->Close();
$word->Quit();
}
sub edit_excel_file{
my $file= $opts{filepath};
my $excel_obj = Win32::OLE->new('Excel.Application', 'Quit') or die "Could not find Word.\n";
my $Book = $excel_obj->Workbooks->Open($file);
my $Sheet = $Book->Worksheets("Sheet2");
$Sheet->Activate();
$Sheet->{Name} = "DidItInPerl";
if ($opts{content} eq 'timestamp'){
$Sheet->Range("a1")->{Value} = $time;
}
else{
$Sheet->Range("a1")->{Value} = $opts{content};
}
$Book->Save();
$Book = $excel_obj->Workbooks->Close();
}
sub edit_pdf_file{
my $file= $opts{filepath};
# Open an existing PDF file
my $pdf = PDF::API2->open($file);
# Add a blank page
my $page = $pdf->page();
# Add some text to the page
my $text = $page->text();
my $font = $pdf->corefont('Helvetica-Bold');
$text->font($font, 20);
$text->translate(200, 700);
$text->text('Hello World!');
$pdf->saveas($file);
}
#Help section
if (defined $opts{help}) { printHelp(); exit; }
sub printHelp
{
my $an = $APPNAME;
print <<EOF;
----------------------------------------------------------
Usage: $an --filepath=<input file> --content=<timestamp|any string>
Operation:
filepath Path to file which you want to edit
content If argument value is timestamp then timestamp will be appended, else provided string
help Usage instructions
Examples:
$an --filepath=E:\\testdata\\testfile.doc
$an --help
----------------------------------------------------------
EOF
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment