运行 Yii 项目中 bash 文件中的 python 脚本
Running a python script within a bash file within a Yii project
我有一个允许导入文件的 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=$XLS
OUTPUT=$CSV
cp $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
DocumentConverter.py:
这可以在这里找到:https://github.com/mirkonasato/pyodconverter。它已被略微修改以具有 python3.
的正确语法
好的,问题是,当 运行 来自终端的 php 代码时,它正确地从 excel 文件创建 csv 文件。
然而,当 运行 它从浏览器中运行时,它仍然运行脚本并创建输出文件,但它没有正确地将其转换为 csv。
到目前为止,当我从控制台 运行 向它抛出的每个文件时,它都完美地工作,但是由于某种原因,当它从浏览器中 运行 时,它无法转换文件正确。
对可能出现的问题有什么想法吗?
谢谢 alejandro,权限错误似乎是问题所在。我还需要将 .config/librroffice 文件夹移动到 apaches 主目录中。
我有一个允许导入文件的 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=$XLS
OUTPUT=$CSV
cp $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
DocumentConverter.py: 这可以在这里找到:https://github.com/mirkonasato/pyodconverter。它已被略微修改以具有 python3.
的正确语法好的,问题是,当 运行 来自终端的 php 代码时,它正确地从 excel 文件创建 csv 文件。
然而,当 运行 它从浏览器中运行时,它仍然运行脚本并创建输出文件,但它没有正确地将其转换为 csv。
到目前为止,当我从控制台 运行 向它抛出的每个文件时,它都完美地工作,但是由于某种原因,当它从浏览器中 运行 时,它无法转换文件正确。
对可能出现的问题有什么想法吗?
谢谢 alejandro,权限错误似乎是问题所在。我还需要将 .config/librroffice 文件夹移动到 apaches 主目录中。