Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Editing an existing excel

by thonny (Initiate)
on Oct 26, 2021 at 03:21 UTC ( [id://11138045]=perlquestion: print w/replies, xml ) Need Help??

thonny has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to Perl scripting. I have an existing excel file and I have to export values from a text file to that excel file. The excel file is like a template that generates histogram based on the values exported from the text file. Can i get some help on how to edit the existing excel file via Perl scripting. Below is the Perl script i have for now.

use strict; use warnings; use lib; use Excel::Writer::XLSX; use IO::Uncompress::Gunzip qw(gunzip $GunzipError); { my $output_fn = 'result.xlsx'; my $input_fn = 'input.txt.gz'; my $workbook = Excel::Writer::XLSX->new( $output_fn ); my $worksheet = $workbook->add_worksheet(); my $zip = IO::Uncompress::Gunzip->new( $input_fn ) or die "gunzip failed: $GunzipError\n"; my $col = 0; my $row = 1; while (!$zip->eof()) { my $line = $zip->getline(); chomp($line); next if $line !~ /\S/; # skip empty lines my $value = $line; $worksheet->write( $row, $col, $value ); $row++; } $workbook->close(); }

Replies are listed 'Best First'.
Re: Editing an existing excel
by kcott (Archbishop) on Oct 26, 2021 at 10:06 UTC

    G'day thonny,

    Welcome to the Monastery.

    It seems to me that you've taken code from elsewhere and just added it to your script without understanding what it does.

    • use lib; — What do think this does? It looks like you don't even need this line (or a variation of it). See the lib documentation.
    • Excel::Writer::XLSX->new(...); — You should be checking that this worked (i.e. file found, could be opened, etc.). See "Excel::Writer::XLSX new()" for more about that.
    • my $col = 0; — This never changes, i.e. it's invariant, so why use a variable. You could just use 0 in the write() method.
    • my $row = 1; — When numbers are used, the $row (and also $col) in write($row, $col, ...) are zero-based. Did you really want to start outputting to the second row? See "Excel::Writer::XLSX Cell notation" for more about that.
    • chomp($line); — This isn't a big problem but there's little point in carrying out processing on a line that you're about to skip. Put this after the next ... statement. That would better reflect the logical processing and indicate that you've actually considered this. You may even get a minor speed improvement; although, unless your input has a huge number of lines, that's likely to be negligible.
    • my $value = $line; — What is the purpose of this? You could just use $line in the write() method and scrap this statement.
    • $row++; — This is more a matter of style and personal taste than any real problem. I'd normally use write($row++, ...) and not include $row++; as a separate statement.

    So, there's seven things you can look at and potentially improve.

    Now, for us to provide you with help, you need to tell us what the problem is. Show us a few representative lines from the uncompressed input.txt.gz file. Show us how those lines appear in the spreadsheet. Show us how you really wanted those lines to appear in the spreadsheet.

    I also suggest you read "How do I post a question effectively?" to see the type of information that we need, and how to present it (e.g. verbatim error messages within <code>...</code> tags).

    — Ken

      I will improve on the suggestions given. The problem I am facing is, with the script I have, it deletes the existing histogram in the excel file. The input.txt.gz file looks something like this:
      0.1 0.2 0.3 0.4 0.5
      Those lines should just appear in the column A of excel sheet. I wish I can attach image here butI cannot. Hope you can understand what I am trying to say.
Re: Editing an existing excel
by tangent (Parson) on Oct 26, 2021 at 13:19 UTC
    You can't edit an Excel file using Excel::Writer::XLSX - you can only create a new empty one and write to it. You can read an existing file using Spreadsheet::ParseXLSX but again, you can't edit.

    You can create charts using Excel::Writer::XLSX (see Excel::Writer::XLSX::Chart) but I can't see a histogram there, only bar charts.

      Yes. My first attempt was to edit the existing excel file. When I failed to do so, I tried to generate histogram with the Perl script itself but I couldnt find way to plot histogram.

        Plotting a histogram with Perl can be achieved using GD::Graph::histogram or via Chart::Gnuplot. I will demonstrate the latter since the former already has a demo.

        use Chart::Gnuplot; my @counts = (10,11,20,5,13,19); my @labels = ('a'..'f'); my $dataSet = Chart::Gnuplot::DataSet->new( xdata => \@labels, ydata => \@counts, title => "Histogram", style => "histograms", ); # Create chart object and specify the properties of the chart my $chart = Chart::Gnuplot->new( title => "Simple testing", xlabel => "My x-axis label", ylabel => "My y-axis label", # this dumbs an ASCII histogram to console #terminal => 'dumb', # this plots it as a PNG output => 'x.png', terminal => 'png', ); # Plot the data set on the chart $chart->plot2d($dataSet);

        bw, bliako

      Question: Is it possible to add new sheets to an existing Excel file?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: Editing an existing excel (crossposted)
by LanX (Saint) on Oct 26, 2021 at 08:07 UTC
      My apologies. I asked to help on how to plot charts.
        Please be clearer.

        I have to guess...

        Do you want to add new rows to an existing sheet, and expect the existing charts to be updated?

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found