在Yii项目中的bash文件中运行python脚本

时间:2015-03-27 08:22:55

标签: bash python-3.x yii php-5.4

我有一个允许导入文件的Yii项目。

在这个项目中,我调用以下命令尝试将xls文件转换为csv:

$file = fopen($model->importfile->tempname,'r');
$filetype = substr($model->importfile, strrpos($model->importfile, '.')+1);
if ($filetype === 'xls')
{
    $tempxls = $model->importfile->tempname;
    $outputArr = array();
    exec(Yii::app()->basePath."/commands/xlstocsv.sh " . $tempxls, $outputArr);
    PropertiesController::xlsToConsoleV7Format($tempxls, $log);
}

xlstocsv.sh:

#!/bin/bash
# Try to autodetect OOFFICE and OOOPYTHON.
OOFFICE=`ls /usr/bin/libreoffice /usr/lib/libreoffice/program/soffice /usr/bin/X11/libreoffice | head -n 1`
OOOPYTHON=`ls /usr/bin/python3 | head -n 1`
XLS='.xls'
CSV='.csv'
INPUT=$1$XLS
OUTPUT=$1$CSV

cp $1 $INPUT

if [ ! -x "$OOFFICE" ]
then
    echo "Could not auto-detect OpenOffice.org binary"
    exit
fi
if [ ! -x "$OOOPYTHON" ]
then
    echo "Could not auto-detect OpenOffice.org Python"
    exit
fi
echo "Detected OpenOffice.org binary: $OOFFICE"
echo "Detected OpenOffice.org python: $OOOPYTHON"
# Start OpenOffice.org in listening mode on TCP port 2002.
$OOFFICE "-accept=socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" -norestore -nofirststartwizard -nologo -headless &
# Wait a few seconds to be sure it has started.
sleep 5s
# Convert as many documents as you want serially (but not concurrently).
# Substitute whichever documents you wish.
$OOOPYTHON /fullpath/DocumentConverter.py $INPUT $OUTPUT
# Close OpenOffice.org.

cp $OUTPUT $1

DocumentConverter.py: 这可以在这里找到:https://github.com/mirkonasato/pyodconverter。它已被略微修改为具有python3的正确语法。

好的,问题是,当从终端运行php代码时,它正确地从excel文件创建csv文件。

但是,当从浏览器中运行它时,它仍然运行脚本并创建输出文件,但它没有正确地将其转换为csv。

对于从控制台运行时到目前为止所抛出的每个文件,它都完美无缺,但出于某种原因,当从浏览器中运行它时,它无法正确转换文件。

任何可能出错的想法?​​

1 个答案:

答案 0 :(得分:1)

感谢alejandro,许可错误似乎是个问题。我还需要将.config / librroffice文件夹移动到apaches主目录。