Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl/postscript csv label maker

Reply
Thread Tools

perl/postscript csv label maker

 
 
luser-ex-troll
Guest
Posts: n/a
 
      03-05-2009
Has this already been done better?
Surely this idea isn't new?
This is a project for my mom, for whom
MSWord and Excel no longer play nicely together.
-- luser-ex-troll

#!/usr/bin/perl
# labels.pl reads csv records in the form:
# "Name","123 Street","Apt","City, ST ZIP"
# from a file named "addr.csv" and
# produces a postscript program on stdout
# suitable for piping into ps2pdf or
# even distiller, maybe
# CAVEATS: prototype produces at most
# one page. label and sheet sizes are hard-coded
# at 3 3-inch-width labels which are 4*17 points
# high. I don't have any real ones on hand
# to measure.

$IN = "addr.csv";
open IN or die "Can't open input: $!";

print <<PROLOG;
%!
/in{72 mul}def
/LEAD 17 def
/LM 10 def
/count 0 def
/TM 11 in LEAD sub def
/Palatino-Roman 15 selectfont

[
PROLOG

while(<IN>) {
s/\n$//;
s/^"/\[\(/;
s/","/\)\(/g;
s/"$/\)\]/;
s/$/\n/;
print;
}

print <<PSPROGRAM;
]

{ %each record
LM TM moveto
{ %each address line
dup () eq { pop } { % skip empties
show % paint the string
%currentpoint exch = = = % diagnostic
LM currentpoint exch pop LEAD sub moveto % reposition
} ifelse
} forall
/LM LM 3 in add store % move over
/count count 1 add store
count 3 mod 0 eq { % back to left after each 3
/LM 10 store /TM TM LEAD 4 mul sub store
} if
} forall

showpage

PSPROGRAM
 
Reply With Quote
 
 
 
 
macropod
Guest
Posts: n/a
 
      03-07-2009
You could, of course, fix the MS Office installation instead of giving your mom something sp arcane to play with.

--
Cheers,
macropod


"luser-ex-troll" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Has this already been done better?
> Surely this idea isn't new?
> This is a project for my mom, for whom
> MSWord and Excel no longer play nicely together.
> -- luser-ex-troll
>
> #!/usr/bin/perl
> # labels.pl reads csv records in the form:
> # "Name","123 Street","Apt","City, ST ZIP"
> # from a file named "addr.csv" and
> # produces a postscript program on stdout
> # suitable for piping into ps2pdf or
> # even distiller, maybe
> # CAVEATS: prototype produces at most
> # one page. label and sheet sizes are hard-coded
> # at 3 3-inch-width labels which are 4*17 points
> # high. I don't have any real ones on hand
> # to measure.
>
> $IN = "addr.csv";
> open IN or die "Can't open input: $!";
>
> print <<PROLOG;
> %!
> /in{72 mul}def
> /LEAD 17 def
> /LM 10 def
> /count 0 def
> /TM 11 in LEAD sub def
> /Palatino-Roman 15 selectfont
>
> [
> PROLOG
>
> while(<IN>) {
> s/\n$//;
> s/^"/\[\(/;
> s/","/\)\(/g;
> s/"$/\)\]/;
> s/$/\n/;
> print;
> }
>
> print <<PSPROGRAM;
> ]
>
> { %each record
> LM TM moveto
> { %each address line
> dup () eq { pop } { % skip empties
> show % paint the string
> %currentpoint exch = = = % diagnostic
> LM currentpoint exch pop LEAD sub moveto % reposition
> } ifelse
> } forall
> /LM LM 3 in add store % move over
> /count count 1 add store
> count 3 mod 0 eq { % back to left after each 3
> /LM 10 store /TM TM LEAD 4 mul sub store
> } if
> } forall
>
> showpage
>
> PSPROGRAM

 
Reply With Quote
 
 
 
 
luser-ex-troll
Guest
Posts: n/a
 
      03-07-2009
On Mar 7, 4:46*am, "macropod" <(E-Mail Removed)> wrote:
> You could, of course, fix the MS Office installation instead of giving your mom something sp arcane to play with.
>
> --
> Cheers,
> macropod


Agreed. But I wanted to have a plan B ready, and I
thought two mutually-embracing languages was interesting.
Besides she's never been happy with the Word+Excel
solution, particularly the word side. When it stopped
working entirely, I thought a different tactic might
be more effective. This way would retain the part she
likes (maintaining the database in excel), and replace
the part she don't (which already required another
program).

Since she already has distiller, she won't need
ghostscript, just perl. everybody needs perl.


luxexterol
 
Reply With Quote
 
luser-ex-troll
Guest
Posts: n/a
 
      03-17-2009
Mommy likes it!
Double-clicking a shortcut is ultimately easier
than any Wizard M$Word can concoct.
Wait till I show her how to edit the file with
a text-editor instead of Excel!

Version 0.2beta:

#!/usr/bin/perl
# labels.pl reads csv records in the form:
# "Name","123 Street","Apt","City, ST ZIP"
# from files named on the command line or
# "addr.csv", if none are specified, and
# produces a postscript program on stdout
# containing records in the form:
# [(Name)(123 Street)(Apt)(City, ST ZIP)]
# suitable for piping into ps2pdf or distiller.
# eg.$ labels|ps2pdf - out.pdf
# CAVEATS:
# coded for Avery 5160 2.5935" x 1"
# letter-sheet labels 3 x 10 / page

if ($#ARGV == -1) { $ARGV[0] = "addr.csv"; } #default argument

print <<PROLOG;
%!
% persuade acrobat reader to obey the numbers
[ {Catalog} << /ViewerPreferences << /PrintScaling /None >> >> /PUT
pdfmark
/in{72 mul}def % tom thumb's shoe size

% measurements
/H 1 in def % how big
/W 2.5935 in def % they are
/Palatino-Roman 16 selectfont % set text 16/
/LEAD H 4 div def % 18
/LM 0.1875 10 add def % edge + padding
/TM 11 in 0.5 in sub LEAD sub def % top - margin - line

% control
/count 0 def
/perLINE 3 def
/perPAGE perLINE 10 mul def

{ %make array as executable to avoid stackoverflow
PROLOG

$\ = $/;
while(<>) {
chop;
s/^"/\{\(/;
s/","/\)\(/g;
s/"$/\)\}/;
print;
}

print <<PSPROGRAM;
} cvlit

{ % each record
cvlit LM TM moveto
{ % each address line
dup () eq { pop } { % skip empties
%dup currentpoint exch = = = % diagnostic
show % paint the string
LM currentpoint exch pop LEAD sub moveto % reposition
} ifelse
} forall
/LM LM W add store % move over
/count count 1 add store
count perLINE mod 0 eq { % back to left after each 3
/LM 10 store
/TM TM H sub store
} if
count perPAGE mod 0 eq { % print a page when it's full
showpage
/LM 10 store
/TM 11 in LEAD sub store
} if
} forall

count perPAGE mod 0 ne { showpage } if
%the end
PSPROGRAM

#eof labels


sample input (addr.csv):
"M. Joshua Ryan","1234 Streetname","","City, ST 12345"
"M. Joshua Ryan","4567 Streetname","","City, ST 23456"
"M. Joshua Ryan","7890 Streetname","","City, ST 34567"
"M. Joshua Ryan","1234 Streetname","","City, ST 45678"
"M. Joshua Ryan","4567 Streetname","","City, ST 56789"
"M. Joshua Ryan","7890 Streetname","","City, ST 67890"
"M. Joshua Ryan","7890 Streetname","","City, ST 67890"
"","","",""


sample output:
%!
% persuade acrobat reader to obey the numbers
[ {Catalog} << /ViewerPreferences << /PrintScaling /None >> >> /PUT
pdfmark
/in{72 mul}def % tom thumb's shoe size

% measurements
/H 1 in def % how big
/W 2.5935 in def % they are
/Palatino-Roman 16 selectfont % set text 16/
/LEAD H 4 div def % 18
/LM 0.1875 10 add def % edge + padding
/TM 11 in 0.5 in sub LEAD sub def % top - margin - line

% control
/count 0 def
/perLINE 3 def
/perPAGE perLINE 10 mul def

{ %make array as executable to avoid stackoverflow
{(M. Joshua Ryan)(1234 Streetname)()(City, ST 12345)}
{(M. Joshua Ryan)(4567 Streetname)()(City, ST 23456)}
{(M. Joshua Ryan)(7890 Streetname)()(City, ST 34567)}
{(M. Joshua Ryan)(1234 Streetname)()(City, ST 4567}
{(M. Joshua Ryan)(4567 Streetname)()(City, ST 56789)}
{(M. Joshua Ryan)(7890 Streetname)()(City, ST 67890)}
{(M. Joshua Ryan)(7890 Streetname)()(City, ST 67890)}
{()()()()}
} cvlit

{ % each record
cvlit LM TM moveto
{ % each address line
dup () eq { pop } { % skip empties
%dup currentpoint exch = = = % diagnostic
show % paint the string
LM currentpoint exch pop LEAD sub moveto % reposition
} ifelse
} forall
/LM LM W add store % move over
/count count 1 add store
count perLINE mod 0 eq { % back to left after each 3
/LM 10 store
/TM TM H sub store
} if
count perPAGE mod 0 eq { % print a page when it's full
showpage
/LM 10 store
/TM 11 in LEAD sub store
} if
} forall

count perPAGE mod 0 ne { showpage } if
%the end

lxt
(yes, that's my real name, mostly)
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-17-2009

["Followup-To:" header set to comp.lang.perl.misc.]


luser-ex-troll <(E-Mail Removed)> wrote:

> #!/usr/bin/perl


Ask for all the help you can get:

use strict;
use warnings;


> if ($#ARGV == -1) { $ARGV[0] = "addr.csv"; } #default argument



$ARGV[0] = "addr.csv" unless defined $ARGV[0]; # default argument


> while(<>) {
> chop;



chop() was how we removed newlines in the mid-90s.

chomp() is how we remove newlines nowadays.


> s/^"/\{\(/;
> s/","/\)\(/g;
> s/"$/\)\}/;



The replacement part of s/PATTERN/REPLACEMENT/ is a string.

None of those backslashed characters are special in strings,
so none of those backslashes are needed.

s/^"/{(/;
s/","/)(/g;
s/"$/)}/;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      03-17-2009
On 2009-03-17, Tad J McClellan <(E-Mail Removed)> wrote:
>> #!/usr/bin/perl

>
> Ask for all the help you can get:
>
> use strict;
> use warnings;


Or, better, add -w ot the #!-line...

>> if ($#ARGV == -1) { $ARGV[0] = "addr.csv"; } #default argument


> $ARGV[0] = "addr.csv" unless defined $ARGV[0]; # default argument


I would write

@ARGV = "addr.csv" unless @ARGV;

[ Unfortunately, the most logical @ARGV ||= "addr.csv" does not
compile... If it would, the scalar/list context determination
might get in the way, too... ]

>> s/^"/\{\(/;
>> s/","/\)\(/g;
>> s/"$/\)\}/;


> The replacement part of s/PATTERN/REPLACEMENT/ is a string.


Still, one should keep in mind that backslashes may help with "less
capable" editors...

Ilya
 
Reply With Quote
 
luser-ex-troll
Guest
Posts: n/a
 
      03-17-2009
On Mar 16, 9:35*pm, Tad J McClellan <(E-Mail Removed)> wrote:
> ["Followup-To:" header set to comp.lang.perl.misc.]
>
> luser-ex-troll <(E-Mail Removed)> wrote:
> > #!/usr/bin/perl

>
> Ask for all the help you can get:
>
> * * use strict;
> * * use warnings;
>
> > if ($#ARGV == -1) { $ARGV[0] = "addr.csv"; } #default argument

>
> * * $ARGV[0] = "addr.csv" unless defined $ARGV[0]; # default argument
>
> > while(<>) {
> > * * chop;

>
> chop() was how we removed newlines in the mid-90s.
>
> chomp() is how we remove newlines nowadays.
>
> > * * s/^"/\{\(/;
> > * * s/","/\)\(/g;
> > * * s/"$/\)\}/;

>
> The replacement part of s/PATTERN/REPLACEMENT/ is a string.
>
> None of those backslashed characters are special in strings,
> so none of those backslashes are needed.
>
> * * s/^"/{(/;
> * * s/","/)(/g;
> * * s/"$/)}/;
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


Excellent. Thank you.
I was transcribing my memory of vi substitutions
while simultaneously relearning perl. It went though
some very ugly stages.

lxt
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      03-17-2009
In article <(E-Mail Removed)>,
Ilya Zakharevich <(E-Mail Removed)> wrote:
>On 2009-03-17, Tad J McClellan <(E-Mail Removed)> wrote:
>>> #!/usr/bin/perl

>>
>> Ask for all the help you can get:
>>
>> use strict;
>> use warnings;

>
>Or, better, add -w ot the #!-line...
>
>>> if ($#ARGV == -1) { $ARGV[0] = "addr.csv"; } #default argument

>
>> $ARGV[0] = "addr.csv" unless defined $ARGV[0]; # default argument

>
>I would write
>
> @ARGV = "addr.csv" unless @ARGV;


I would write

@ARGV = ('addr.csv') if @ARGV == 0;

The parens don't actually cause a list context, but they're visually
reminiscent. In C, where {...} was allowed but not required around
one statement, I used {...} anyway.

I use '...' unless I need variable interpolation.

--
Tim McDaniel, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      03-17-2009
On 2009-03-17 06:00, Ilya Zakharevich <(E-Mail Removed)> wrote:
> On 2009-03-17, Tad J McClellan <(E-Mail Removed)> wrote:
>>> #!/usr/bin/perl

>>
>> Ask for all the help you can get:
>>
>> use strict;
>> use warnings;

>
> Or, better, add -w ot the #!-line...


Ok, I'll bite: How is -w better than "use warnings"?

hp
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-17-2009
(E-Mail Removed) (Tim McDaniel) wrote in
news:gpoich$ft1$(E-Mail Removed):

> In article <(E-Mail Removed)>,
> Ilya Zakharevich <(E-Mail Removed)> wrote:
>>On 2009-03-17, Tad J McClellan <(E-Mail Removed)> wrote:
>>>> #!/usr/bin/perl
>>>
>>> Ask for all the help you can get:
>>>
>>> use strict;
>>> use warnings;

>>
>>Or, better, add -w ot the #!-line...
>>
>>>> if ($#ARGV == -1) { $ARGV[0] = "addr.csv"; } #default argument

>>
>>> $ARGV[0] = "addr.csv" unless defined $ARGV[0]; # default
>>> argument

>>
>>I would write
>>
>> @ARGV = "addr.csv" unless @ARGV;

>
> I would write
>
> @ARGV = ('addr.csv') if @ARGV == 0;



I would write:

@ARGV = qw( addr.csv ) unless @ARGV;

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Modern Stand Alone Photo Image Organizer/Album Maker/Slide Show Maker JOHNCHAP2 Digital Photography 3 02-05-2013 11:54 PM
Video card does not meet minimum requirments for windows dvd maker and movie maker Travis Computer Information 1 10-01-2006 12:10 AM
how to tell display adapter maker when no maker listed on it effi Computer Information 3 10-26-2004 08:43 AM
Re: csv writerow creates double spaced excel csv files Skip Montanaro Python 0 02-13-2004 08:50 PM
csv writerow creates double spaced excel csv files Michal Mikolajczyk Python 0 02-13-2004 08:38 PM



Advertisments