how to read data from spreadsheet with .xlsm format in perl? - perl

I want read data from spreadsheet ,which is .xlsm format.I am unable to access it.its showing blank while access the data .so i want how to access .xlsm format in perl.
Here is what I have tried:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::Read;
use Spreadsheet::read qw(ReadData);
my $book = ReadData ('C:\Perl64\bin\sample.xlsm');
foreach my $line(1..1000) {
my #rows =Spreadsheet::Read::cellrow($book->[0],"$line");
print "#rows";
}

seems the module you used doesn't work on xlsm files
consider using Spreadsheet::Reader::ExcelXML instead ;)
http://search.cpan.org/~jandrew/Spreadsheet-Reader-ExcelXML/lib/Spreadsheet/Reader/ExcelXML.pm
also beware, you should use push instead of #rows = ..., this way, you won't overwrite #rows at each for iteration
when you try to open a file you should add or die "error message"; at the end of the open, this way you will see if the file opened correctly
my $book = ReadData ('C:\Perl64\bin\sample.xlsm') or die "error while opening the file"; triggers the error with your example code

Related

get the whole content from the web site using perl script

I am a new hire in my company and first time I am working on Perl.
I get a task in which I find IP-Reputation from this link: https://www.talosintelligence.com/reputation_center/lookup?search=27.34.246.62
But in perl when we use:
#!/bin/perl
use strict;
use warnings;
use WWW::Mechanize;
open FILE1, ">./Reports/Reputation.txt" or die "Cannot open Reputation.txt!";
my $mech = WWW::Mechanize->new( autocheck => 1 );
my $url="https://www.talosintelligence.com/reputation_center/lookup?search=27.34.246.62";
$mech->get($url);
print $mech->status();
my $content = $mech->content();
open FILE1, ">./Reports/Reputation.txt" or die "Cannot open Reputation.txt!";
print FILE1 ($content);
close FILE1;
print "\nIP Reputation Report Generated \n";
I don't get the whole content. What can I do to get this?
Contents are loading from JavaScript. So you can't crawl the content using simple methods.
There is two option for this kind of situation.
1) Some API contains the original data and JavaScript loads/formating the data in front end. If you want to parse the JavaScript loading content try to use the
WWW::Mechanize::Firefox
2) Try to figure out from where it is loading, for your IP following link has the corresponding data, which is JSON formated, so parse the content using JSON module. it is so simple compare to using RegEx.
https://www.talosintelligence.com/sb_api/query_lookup?query=%2Fapi%2Fv2%2Frelated_ips%2Fip%2F&query_entry=27.34.246.62

Reading a CSV file and writing to a CSV file

use Text::CSV;
$csv = Text::CSV->new;
open(HIGH, "+>Hardtest.csv") || die "Cannot open ticket $!\n"; #reads the high file
while(<HIGH>)
{
print "Printing High Priority Tickets ...\n";
sleep(1);
print <HIGH>;
}
close(HIGH);
here is my code, i am trying to read a csv and write to it, however i cant seem to read the CSV file, help would be appreciated, thanks!
OK, lots of things here.
Always use strict and use warnings.
You're opening the CSV file write mode (append mode?). Don't do that, if you're just reading from it.
Don't use || die, use or die.
Finally, don't print <HIGH>, instead print $_.
I've modified your code a bit:
#!/usr/bin/perl -w
use strict;
use Text::CSV;
my $csv = Text::CSV->new;
open(HIGH, "+<Hardtest.csv") || die "Cannot open ticket $!\n"; #reads the high file
while(<HIGH>)
{
print "Printing High Priority Tickets ...\n";
print $_;
sleep(1);
}
print HIGH "9,10,11,12\n";
close(HIGH);
Let me explain:
1. "+>" will open the file in read/write mode, BUT will also overwrite the existing file. Hence, In your code, while loop is never entered. I've changed that to "+<" which means read/write in append mode.
2. Second last statement, in above code, will append new content to the CSV file.

How to modify the xml value that should reflect in the same xml file using perl?

Here is the perl script I have written to modify the particular value from the xml file — neo-datasource.xml.
I can print the modified xml contents in the output console using ->toString, but I wish to have these changes to be reflected in the same xml file called neo-datasource.xml instead of printing it in console.
Could you please share your ideas?
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig->new( keep_spaces => 1 );
$twig->parsefile('C:\Users\IBM_ADMIN\Desktop\dbautomate\neo-datasource.xml');
my ($class_string) = $twig->findnodes('//var[#name="d1new1d1"]/struct[#type="coldfusion.server.ConfigMap"]/var[#name="password"]/string');
$class_string->set_text('NoDatabase');
print $twig->toString;
You're very nearly there — you just need to open the file for writing and print to it (not to STDOUT), something like:
open(my $fh, '>', 'C:\Users\IBM_ADMIN\Desktop\dbautomate\neo-datasource .xml')
or die "Cannot open XML file for writing\n";
$fh->print($twig->toString);

Reading XLS file using Perl

This is my Perl script
#!/usr/bin/perl -w
use strict;
use warnings;
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel;
my $workbook = Spreadsheet::ParseExcel->new("/home/Admin/Desktop/RP_processed_Address_withsubscriptionID (1).xls");
my $workbook1 = Spreadsheet::WriteExcel->new("/home/Admin/Desktop/new.xls");
open(my$old, '<', "$workbook") or die "oops!";
open(my$new, '>', "$workbook1") or die "ooops!";
while (my$line = <$workbook>) {
print $workbook1 $line
}
When I run this Script I'm getting following error
Odd number of elements in hash assignment at /usr/local/share/perl5/Spreadsheet/ParseExcel.pm line 167.
oops! at sample.pl line 9.
I'm not getting any idea where is script is going wrong . Please help me how to resolve this error
your suggestions will be appreciable.
You are not reading any docs again. You copy and paste code and don't understand the basics of what you do. Why are you opening files using open when you already open them using the two modules? Why do you write a line manually? This is not how excel data works, this is not how the modules work. Stop guessing. Learn what you're doing. This will never work.
Take a look at CPAN for Spreadsheet::ParseExcel
You need to access the worksheets within the workbook object you've created and determine which you would like to parse data from. From here you can access cells using the column/width coordinate system. You don't need to use 'open' as the ParseExcel and WriteExcel already do this.
my $sheet = $workbook1->worksheet('Sheet1');
my $cell = $sheet->get_cell( 0, 0 );
my $cell_value = $cell->value();
Is it a bit more clear on what you need to do?

Getstore to Buffer, not using temporary files

I've started Perl recently and mixed quite a bit of things to get what I want.
My script gets the content of a webpage, writes it to a file.
Then I open a filehandler, plug the file report.html in (sorry i'm not english, i don't know how to say it better) and parse it.
I write every line i encounter to a new file, except lines containing a specific color.
It works, but I'd like to try another way which doesn't require me to create a "report.html" temporary file.
Furthermore, I'd like to print my result directly in a file, I don't want to have to use a system redirection '>'. That'd mean my script has to be called by another .sh script, and I don't want that.
use strict;
use warnings;
use LWP::Simple;
my $report = "report.html";
getstore('http://test/report.php', 'report.html') or d\
ie 'Unable to get page\n';
open my $fh2, "<$report" or die("could not open report file : $!\n");
while (<$fh2>)
{
print if (!(/<td style="background-color:#71B53A;"/ .. //));
}
close($fh2);
Thanks for your help
If you have got the html content into a variable, you can use a open call on this variable. Like:
my $var = "your html content\ncomes here\nstored into this variable";
open my $fh, '<', \$var;
# .. just do the things you like to $fh
You can try get function in LWP::Simple Module ;)
To your sencond question, use open like open $fh, '<', $filepath. you can use perldoc -f open to see more info.