[Zope] RE: CSV (Was: Perl scripts)
Warnes, Gregory R
gregory_r_warnes@groton.pfizer.com
Fri, 18 Oct 2002 10:04:21 -0400
> So back to the original topic: Is there any example how to use an
> external Perl script and shipping some parameters to it? I just found
> the statement that it is possible. I wonder if anybody tried ...
I regularly use a Perl script called from Zope to do the opposite
conversion: *from* xls *to* csv. Initially, I used this code in an
external python script, but I've now created a Zope product called 'CSVFile'
which checks the file extension and automatically converts uploaded files
from xls to csv if required. Here is the appropriate code snippet:
##
## Configuration
##
## Perl Path
PERL_PATH='/usr/local/bin/perl'
## Location of perl script 'xls2csv.pl'
XLS2CSV_PATH = '/usr/local/zope/current/Extensions/xls2csv.pl'
# ... other stuff here ...
def
ExcelToCSV(ExcelData=None,ExcelName='ExcelFile.xls',CSVName='CSVFile.csv'):
"""Translate the first worksheet of a Microsoft Excel file to CSV
format by calling the perl script 'xls2csv.pl'
"""
if ExcelData == None:
return None
dirinfo = do_tmpdir()
excelfile = open(ExcelName,"w")
excelfile.write(ExcelData)
excelfile.close()
HERE = dirinfo['tmpdir']
command = "%s %s %s %s 1 2>1 > tmp.out " % (PERL_PATH,
XLS2CSV_PATH,
HERE + ExcelName,
HERE + CSVName )
# print "Executing command '%s'" % command
retval = os.system( command )
if retval==0:
csvfile = open(CSVName,"r")
CSVData = csvfile.read()
csvfile.close()
else:
CSVData = None
clean_tmpdir(dirinfo)
return CSVData
As an aside, I use double-quotes to bracket each cells contents and comma
separation. With this, I haven't had any problems here with MS-Excel
failing to properly convert csv files when opening them -- with any of IE,
Mozilla, Netscape 4 on W2K.
-Greg
LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.