实现功能:网页点击按钮后,后端触发exe文件,exe文件是用python编写,功能是把word文档转换为pdf文档。VS里面能正常运行,而在IIS里面浏览网页访问时,发生异常。网上找了办法,说是没有足够权限,我也都在应用池中设置了Local Service或administrator权限,MIME类型也有doc,exe,pdf 类型。
IIS里浏览,发现能触发exe文件,能遍历到文件夹的word文件,但是打开并转换pdf时,报:AttributeError: 'NoneType' object has no attribute 'ExportAsFixedFormat' [12296]
C#的调用程序:
Process p = new Process();
string output = "";
try
{
string pyexePath = System.Web.HttpContext.Current.Server.MapPath("~/Attachment/Temp/") + "ReWorkFilesToPDF.exe";
p.StartInfo.FileName = pyexePath;
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(pyexePath);// 来获取exe的工作路径:
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();//启动进程
output = pyexePath+"OutPut:" + p.StandardOutput.ReadToEnd();
while (!p.HasExited)
{
p.WaitForExit(1000);
}
string errormsg = p.StandardError.ReadToEnd();
output = output + " error:" + errormsg;
p.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
}
else
{
throw new Exception(output);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
p.Close();
p.Dispose();
}
p.Close();
------------------------------------------------------------------------------------------------------
python 程序:
import os from win32com.client import Dispatch, constants, gencache, DispatchEx
def doc(self, filename): ''' doc 和 docx 文件转换 ''' name = os.path.basename(filename).split('.')[0] + '.pdf' exportfile = os.path.join(self._export_folder, name) print('保存 PDF 文件:', exportfile) gencache.EnsureModule('{00020905-0000-0000-C000-000000000046}', 0, 8, 4) w = Dispatch("Word.Application") doc = w.Documents.Open(filename) doc.ExportAsFixedFormat(exportfile, constants.wdExportFormatPDF, Item=constants.wdExportDocumentWithMarkup, CreateBookmarks=constants.wdExportCreateHeadingBookmarks) w.Quit(constants.wdDoNotSaveChanges)