Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

How to generate the .doc file for each row from a csv file

by perladdict (Chaplain)
on Feb 23, 2014 at 09:46 UTC ( #1075879=perlquestion: print w/replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am very happy to tell you that i got some work on perl,from the below code i can read the csv file, from this output
i need to generate the .doc file from each row of the CSV file. The standerd document template is below

Each line of the CSV will have 8 columns like Report Name,Report desciption,Actual filename,Report format,Report execution frequency,cabinet,folder and Apllication name.I have to parse each line of the CSV for obtaining the reportname,Report format,Report description,cabinet,folder and application name from the CSV file and need to create a .doc file for each row of CSV and save with teh report name as in the above .doc template.

Below is the code which i have obtained from this site to read the CSV file
use strict; use warnings; use Text::CSV_XS; my $file = "C:\\Users\\..\\Desktop\\reportlist.csv"; open my $fh, "<", $file or die "$file: $!"; my $csv = Text::CSV_XS->new ({ binary => 1, # Allow special character. Always set this auto_diag => 1, # Report irregularities immediately }); while (my $row = $csv->getline ($fh)) { print "@$row\n"; } close $fh;
After running the above code it displayes the o/p as below
Report Name Report Description Actual Filename Report Format Report +Execution Frequency Cabinet Folder Application GACCNTS MIS report csv Monthly DBNLRP GACCOUNT IDMS IDEALSCT IDEALIncomingCreditTransfers NVSEPAORT.yyyymmddnn.01 txt Mont +hly DBNLNVRP IDEALSCT MINT OVRSTPRJ OVERSTAP Rejected Transactions SEPAORT.yyyymmddnn.01 txt Mon +thly DBNLRP OVRSTPRJ RANSYS NLACGLST AcceptGiroVolumeperAccount SEPAAGL.yyyymmdd.01 txt Monthly DB +NLRP NLACGRVL MERVA NLACGVLM AcceptGiroVolume SEPAAGV.yyyymmdd.01 txt Monthly DBNLRP NLACG +RLS FMS NVACGLST AcceptGiroVolumeperAccount NVSEPAAGL.yyyymmdd.01 txt Monthly +DBNLNVRP NVACGRVL FMSFIN NVACGVLM AcceptGiroVolume NVSEPAAGV.yyyymmdd.01 txt Monthly DBNLNVRP N +VACGRLS DBCORAL

I have to create the number of .doc file for each row of CSV data and need to save the each documnet as with first field as Report name.Monks please suggest me the way to accomplish this task,This will be an humble request .Monks thanks in advance.

Replies are listed 'Best First'.
Re: How to generate the .doc file for each row from a csv file
by hdb (Prior) on Feb 23, 2014 at 10:09 UTC

    There might be modules to build a fully blown template system, but in a crude fashion you could just do this kind of thing:

    use strict; use warnings; my $row = [ "input", "format", "description", "recdate", "cabinet", "f +older" ]; print <<"DOC"; [DOCUMENT] INPUT=$$row[0] FORMAT=$$row[1] DESCRIPTION=$$row[2] RECDATE=$$row[3] DELETEINPUT=NO NODE_ID=1 VOLUME_ID=1 [CBNTFLDR] CABINET=$$row[4] FOLDER=$$row[5] [USER_DEFINITIONS] Freq:Daily Application: DOC print "\n";

    Of course, you would need to first open your .doc file with the correct name and add the file handle to the print statement.

      Hi HDB, Thanks for the inputs, based on your input i have remodified the script for generating .doc files as below
      #!/usr/bin/perl $file = "C:\\Users\\xxx\\Desktop\\reportlist.csv"; open(FH,"$file") or die"$!\n"; @arr = <FH>; $first = shift(@arr); foreach $line(@arr) { ($reportname,$reportdesc,$actfl,$format,$freq,$cabinet,$folder,$appl) += split(',',$line); #print "$reportname,$reportdesc,$actfl,$format,$freq,$cabinet,$folder, +$appl\n"; @files = "C:\\Users\\xxx\\Desktop\\$reportname.doc"; #print "@files\n"; foreach $doc(@files) { open(FH,'>'.$doc) or die"$!\n"; print FH "\[DOCUMENT\]\n"; print FH "INPUT=$reportname" ."\n"; print FH "FORMAT=$format" . "\n"; print FH "DESCRIPTION=$reportdesc" ."\n"; print FH "RECDATE= " ."\n"; print FH "DELETEINPUT=NO" ."\n"; print FH "NODE_ID=1" ."\n"; print FH "VOLUME_ID=1" ."\n"; print FH " " ."\n"; print FH "[CBNTFLDR]" ."\n"; print FH "CABINET=$cabinet" . "\n"; print FH "FOLDER=$folder" ."\n"; print FH " " ."\n"; print FH "[USER_DEFINITIONS]" ."\n"; print FH "Freq:Daily Application:$appl" ."\n"; print FH " " ."\n"; } close(FH); }
      When i run the above code it is perfectly creating the .doc files for all the csv rows.

        One comment: you have a single .doc file in for each row in your csv file, so you can assign the filename directly to $doc:

        $doc = "C:\\Users\\xxx\\Desktop\\$reportname.doc";

        without any need for a loop over @files.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1075879]
Approved by hdb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2017-02-27 03:35 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (376 votes). Check out past polls.