douquan2023
2015-07-16 06:55
浏览 146
已采纳

问题从php调用python脚本(Python脚本自己工作)

I've got a problem calling python script from php.

I've got a file:

import fnmatch, os, pythoncom, sys, win32com.client

try:
     w = win32com.client.gencache.EnsureDispatch("Word.Application")
     w.Documents.Open(r"path-to-a-file-I-want-to-convert.doc")
     doc = w.ActiveDocument # brakes here
     w.Selection.Find.ClearFormatting()
     w.ActiveDocument.SaveAs(r"save-as-name.docx", FileFormat = 12)
     w.ActiveWindow.Close()

except IOError as (errno, strerror):
     print "I/O error({0}): {1}".format(errno, strerror)
except ValueError:
     print "Could not convert data to an integer."
except:
     print "Unexpected error:", sys.exc_info()[0]
     raise

that converts .doc file into .docx format.

The script works properly until I call it from .php:

$arg = exec('C:\Python27\python.exe "path-to-script.py"', $output);
var_dump($output);

It outputs test lines if I put them there (such as print "sth") but brakes after

doc = w.ActiveDocument 

line and gives "Unexpected error". I would appreciate any suggestions on how I can call that script from .php and why it fails.

图片转代码服务由CSDN问答提供 功能建议

我从php调用python脚本时遇到问题。

我有一个文件:

  import fnmatch,os,pythoncom,sys,win32com.client 
 
try:\  nw = win32com.client.gencache.EnsureDispatch(“Word.Application”)
 w.Documents.Open(r“path-to-a-file-I-want-to-convert.doc”)
 doc = w  .ActiveDocument#brakes here 
 w.Selection.Find.ClearFormatting()
 w.ActiveDocument.SaveAs(r“save-as-name.docx”,FileFormat = 12)
 w.ActiveWindow.Close()
  
except IOError as(errno,strerror):
 print“I / O error({0}):{1}”。format(errno,strerror)
except ValueError:
 print“无法将数据转换为整数 。“
except:
 print”意外错误:“,sys.exc_info()[0] 
 raise 
   
 
 

将.doc文件转换为.docx 格式。

脚本正常工作,直到我从.php调用它:

  $ arg = exec('C:\ Python27 \ python。  exe“path-to-script.py”',$ output); 
var_dump($ output); 
   
 
 

如果我把测试线放在那里它会输出测试线( 例如打印“sth”)但是在

  doc = w.ActiveDocument 
   
 
 

之后制动并给出“ 意外的错误”。 我将不胜感激任何有关如何从.php调用该脚本以及失败原因的建议。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题