Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: not able to retrieve the color of a table cell

by ghosh123 (Monk)
on Jul 18, 2013 at 07:11 UTC ( [id://1044998]=note: print w/replies, xml ) Need Help??


in reply to Re: not able to retrieve the color of a table cell
in thread Reaped: not able to retrieve the color of a table cell

Thanks, it is working now.
But one more thing, how can I write the html content retaining all the color codes of the cells. How can I use the code I have already written to dump the content into excel for this purpose. I need a bit hint.

  • Comment on Re^2: not able to retrieve the color of a table cell

Replies are listed 'Best First'.
Re^3: not able to retrieve the color of a table cell
by poj (Abbot) on Jul 18, 2013 at 07:39 UTC

    Add formats for each color as you encounter them

    my $nrow=0; my %fmt=(); # cell format for my $ts ($te->tables) { for my $row ($ts->rows) { my $ncol=0; for my $el (@$row) { my $tag = $el->find_by_tag_name('font'); my $color = (defined $tag) ? $tag->attr('color') : 'white' ; if (! exists $fmt{$color}){ $fmt{$color} = $workbook->add_format(bg_color=>$color); } $worksheet->write($nrow,$ncol++,$el->as_text,$fmt{$color}) if de +fined $el; } $nrow++; } $nrow++; }
    poj

      Hi poj
      It is working fine for the HTML table I gave with NAME ,ID and RESULT as status. But if the webpage contains the following content then it fails :
      It gives the following error
      Can't call method "tag" without a package or object reference at /usr/bin/perl/lib/HTML/ElementTable.pm line 367.
      The HTML is as follows :

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>DV</title> <link rel="stylesheet" type="text/css" href="/css/tree.css" /> <script src="/js/tree.js" type="text/javascript"></script> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body bgcolor="#FFFFFF"> <br\><TABLE><TR><TD><img align="left" src="/images/ver/logo100.gif" al +t="[LOGO]" /><TD><h1>Reg</h1><h3>MC:Test </h3></TD></TR></TABLE> <br /><p /><a href="ab.pl"><b>[overview]</b></a>&nbsp;&nbsp;<a href="ab.pl?path=/usr/proj">[RUN]</a>&nbsp;&nbsp;<a href="s.pl?path=/usr/proj">[DB]</a>&nbsp;&nbsp;<a href="q.pl?path=/usr/proj">[Report]</a><p /><br /> <FONT SIZE=-1><I>(Click on the hyperlinks in the table headers to see +information on methods [if available])</I></FONT><BR> <TABLE BORDER=1><TR BGCOLOR="#D0D0D0"><TH ALIGN=LEFT NOWRAP>List</TH>< +TH ALIGN=LEFT>St1</TH><TH ALIGN=LEFT>rc</TH><TH ALIGN=LEFT>st2</TH><TH ALIGN=LEFT>st3</TH><TH ALIGN=LEFT>st4</TH><TH ALIGN=LEFT><A HREF="m.pl?path=/usr/proj;task=layverDoc;command=exec"st5</A></TH><TH +ALIGN=LEFT><A HREF="m.pl?path=/usr/proj;task=lw;command=exec">layverShadow</A></TH>< +TH ALIGN=LEFT>lvs</TH><TH ALIGN=LEFT>st6</TH><TH ALIGN=LEFT>st7</TH><TH ALIGN=LEFT>pi</TH><TH ALIGN=LEFT><A HREF="m.pl?path=/usr/proj;task=prefix;command=exec">pix</A></TH></TR> <TR CLASS='node' ID='ck' VALIGN=top><TD ALIGN=LEFT NOWRAP><A CLASS=bla +nk HREF="" ONCLICK="toggleRow(this.parentNode.parentNode.parentNode, +'ck'); return false;">&nbsp;<B>clock_x2</B></A><B></B></TD><TD BGCOLO +R="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></T +D><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR=" +#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD>< +TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD><TD BGCOLOR="#D0 +D0D0"></TD><TD BGCOLOR="#D0D0D0"></TD></TR> <TR CLASS='node' ID='ck@ck' VALIGN=top><TD ALIGN=LEFT NOWRAP>&nbsp;& +nbsp;&nbsp;&nbsp;<B>clock_x2</B><B></B></TD><TD><FONT COLOR="green">O +K</FONT></TD><TD>-</TD><TD><FONT COLOR="green">OK</FONT></TD><TD><FON +T COLOR="green">OK</FONT></TD><TD><FONT COLOR="green">OK</FONT></TD>< +TD><FONT COLOR="green">OK</FONT></TD><TD><FONT COLOR="green">OK</FONT +></TD><TD>-</TD><TD><FONT COLOR="green">OK</FONT></TD><TD><FONT COLOR +="green">OK</FONT></TD><TD><FONT COLOR="green">OK</FONT></TD><TD><FON +T COLOR="green">OK</FONT></TD></TR> </TABLE> <BR><hr> <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%> <TR> <TD WIDTH=42%> <FONT SIZE=-2>&copy; Copyright(c) 1996<BR> Company </FONT></TD> <TD WIDTH=42%> <FONT SIZE=-2>Last modified 2013 <BR> by <A HREF="mailto:res@next.com">res</A> </TD> <TD> <IMG BORDER=0 SRC="/images/ver/ver_dev_team.jpg" ALIGN=RIGHT HSP +ACE=0 VSPACE=0 ALT="[ver]"> </TD> </TR> </TABLE> <p /><br /> </body> </html>

      And my code which is not working for the above html is below. But it is working successfully for the previous html I mentioned.

      #!/usr/bin/perl use strict; use warnings; use Spreadsheet::WriteExcel; use HTML::TableExtract qw(tree); require LWP::UserAgent; use HTML::Element; my $ua = new LWP::UserAgent(); my $request = HTTP::Request->new(GET=>"http://autoreport.nextgen.com"; +); my $response = []; $response = $ua->request($request); $response = $response->content(); my $data = []; @$data = split(/\n/, $response); # Create a new Excel file my $filename = "/home/user/result.xls"; my $workbook = Spreadsheet::WriteExcel->new($filename); # Add a worksheet my $worksheet = $workbook->add_worksheet('exec2'); # Define the format and add it to the worksheet my $format = $workbook->add_format( center_across => 1, bold => 1, size => 10, border => 4, color => 'black', bg_color => 'cyan', border_color => 'black', align => 'vcenter', ); my $htm; foreach(@$data) { $htm .= $_ ; } print "htm $htm \n"; my $te = HTML::TableExtract->new(); # headers => ["ID", "NAME" ,"DOB"] + ); $te->parse($htm); # ($response); my $nrow= 0; my %fmt = (); for my $ts ($te->tables) { print "ts $ts \n"; for my $row ($ts->rows) { my $ncol = 0; for my $el (@$row) { my $tag = $el->find_by_tag_name('font'); my $color = (defined $tag) ? $tag->attr('color'): 'white'; if(! exists $fmt{$color}) { $fmt{$color} = $workbook->add_format(bg_color=>$co +lor); } $worksheet->write($nrow,$ncol++,$el->as_text,$fmt{$col +or}) if defined $el; } $nrow++ } $nrow++ }

        Hi
        I could get rid of the problem by mentioning the depth and count arguments to the new function as :

        my $te = HTML::TableExtract->new(depth=>0,count=>1 );

        Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1044998]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-20 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found