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

I have existing spreadsheets that I want to send to a printer using Perl.
I have tried
copy($file_name, $printer_port);
This sends data to the printer but nothing like the contents of the spreadsheets that I want.
I know I can use
open(LBPOP, '>' . $printer_port});
to open the printer but I do not know what to do after that.
I have seen references to Net::Printer but this module is not on my Perl distribution. Therefore I would prefer not to have to use this.
(I cannot search SuperSearch since this is not ‘working’ at the moment.)
Can any Monk tell me how to do this?

Replies are listed 'Best First'.
Re: Printing spreadsheets
by Sinistral (Monsignor) on Jan 11, 2011 at 15:37 UTC

    You're code is correct, in that you are attempting to copy a binary spreadsheet file directly to the port of a printer. As you discovered, this is not the way to actually have any readable output. What you have to do is to use the print functionality of a spreadsheet application to send formatted printer control language data to the file. That is, you are trying to use Perl to do the equivalent of File->Open, File->Print. There are a couple of ways to approach that:

    1. If you're using Windows, you can use Win32::OLE to control a program such as Excel and send the print command. A good overall example can be found at tek-tips, and right here at Perlmonks Using Win32::OLE and Excel - Tips and Tricks. You'll want to set the Excel visibility to 0 (so you don't see the application when you are running it) and you want to use the PrintOut method of the active sheet to actually print the file
    2. If you're using OpenOffice.org, it has its own programming interface that you can use to open the file and print it.
    3. If you want just the raw data, you could use Spreadsheet::ParseExcel (if you're using an Excel file) to get the cell contents programatically and then use the Perl system command to invoke lpr on the resulting formatted text.

    Because of the operating system specific nature of printing (whether Windows, Mac, or *NIX), you need to tell us:

    • What OS you're using (Win, Mac, Linux)
    • What type of spreadsheet file you're using (Excel, OpenOffice.org, plain text CSV, something else

    if you want truly detailed and good answers

      Thanks for that. Using one of the references and recording a macro in Excel, I found that the following worked.
      $excel = CreateObject OLE "Excel.Application"; $workbook = $excel -> Workbooks -> Open(file name); $workbook->PrintOut();
Re: Printing spreadsheets
by SuicideJunkie (Vicar) on Jan 11, 2011 at 15:27 UTC

    Surely you're not trying to dump a raw xls/ods file into your printer port?

    How/where are you rendering the file into a printable format?

      I did not mean to (dump raw data!)
      I am not doing anything to make the file printable. I guess that is the step I am missing.
      What is the Perl code for that?

        +1 for your honesty! Try Spreadsheet::Read for a generic interface to most spreadsheets (XLS, XLSX, ODS, SXC, CSV).


        Enjoy, Have FUN! H.Merijn