【发布时间】:2023-03-14 08:40:01
【问题描述】:
我有一个允许导入文件的 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。
到目前为止,当我从控制台运行时,这对于我扔给它的每个文件都非常有效,但由于某种原因,从浏览器中运行它时,它无法正确转换文件。
对可能出现的问题有什么想法吗?
【问题讨论】:
-
@james-sunderland 在评论中你提到 DocumentConverter.py 不应该同时使用这是 libreoffice 在监听模式下的问题,因为我想同时使用它和/或你对此有任何来源或更多信息吗?
标签: bash python-3.x yii php-5.4