Open parsasaei opened 2 months ago
Hi,
First of all, it should be mentioned that MS Office is generally not suitable for server-side automation.
I am particularly concerned about whether Word is installed in that Windows environment. Additionally, I think it might be necessary to pass the machine
argument to CreateObject
.
@junkmd
HI,
Word is installed.
About passing machine
to CreateObject
what change I can do on the code?
I passed machine
parameter, but still get that error.
I think this is more of a technical issue related to COM rather than comtypes or Python.
Do you have any references for what you’re trying to do?
I don't sure I got it right about reference you said, but the sample process for making remote procedure call I used is like: https://www.c-sharpcorner.com/article/getting-started-with-remote-procedure-call/ and after get the request from client on the server I calls my method to run script python with c#:
public Word2PDFResult word2pdfConvert(string arguments)
{
var file = "Word2PDF";
//var file = "Word2PDFS";
var fileName = System.IO.Directory.GetFiles(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "*"+ file + "", System.IO.SearchOption.AllDirectories).FirstOrDefault();
var pythonPath = "C:\\Python\\Python312\\Python";
var output = new StringBuilder();
var error = new StringBuilder();
using (var process = new Process())
{
try
{
process.StartInfo.FileName = pythonPath;
process.StartInfo.Arguments = fileName + " " + arguments;
//process.StartInfo.FileName = fileName;
//process.StartInfo.Arguments = " " + arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
//process.StartInfo.Verb = "runas";
//process.StartInfo.WorkingDirectory = Path.GetDirectoryName(fileName);
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (s, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (s, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.Close();
}
}
catch(Exception ex) { process.Kill(); }
}
return new Word2PDFResult { Error = error.ToString(), Output = output.ToString() };
}
Before I make the procedure like RPC service which I mentioned, I called the process with iis directly and when user is not log on on remote the server, I got this error:
Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "D:\Word2PDF_Python\Word2PDFS\__main__.py", line 3, in File "D:\Word2PDF_Python\Word2PDFS\Word2PDF_Python.py", line 123, in cli File "D:\Word2PDF_Python\Word2PDFS\Word2PDF_Python.py", line 72, in convert File "D:\Word2PDF_Python\Word2PDFS\Word2PDF_Python.py", line 23, in windows File "D:\Word2PDF_Python\Word2PDFS\packages\comtypes\client\__init__.py", line 273, in CreateObject File "D:\Word2PDF_Python\Word2PDFS\packages\comtypes\_post_coinit\misc.py", line 149, in CoCreateInstance File "_ctypes/callproc.c", line 1008, in GetResult OSError: [WinError -2147467238] The server process could not be started because the configured identity is incorrect. Check the username and password
When I was connecting to the remote server, the client request was successful, which means it needed to admin user be present on remote server to can client user on web can pass the request.
To confess, I currently have no detail knowledge of RPC.
Recently, I had the opportunity to read some old COM technical documents, but I haven't come up with a modern way to use RPC.
Since this is a topic that requires help from the community, I have added the appropriate label. Please feel free to comment on this issue.
I'm trying to make a RPC service for calling my script which used comtypes package to create a pdf file from docx. When I call my script on the command line prompt I have no any problem, but when I make calling from windows service I get this error from the output: This is my script:
I call my script with this:
python .\\w2PDF .\\word.docx .\\word.pdf
I get this on windows service output:
Traceback (most recent call last):\r\n File \"<frozen runpy>\", line 198, in _run_module_as_main\r\n File \"<frozen runpy>\", line 88, in _run_code\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\__main__.py\", line 3, in <module>\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\Word2PDF_Python.py\", line 127, in cli\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\Word2PDF_Python.py\", line 76, in convert\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\Word2PDF_Python.py\", line 41, in windows\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\packages\\comtypes\\_meta.py\", line 14, in _wrap_coclass\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\packages\\comtypes\\_post_coinit\\unknwn.py\", line 520, in QueryInterface\r\nValueError: NULL COM pointer access
I couldn't find the problem, When I change the user log on to
Network Service
orLocal Service
above error changes to access is denied :Traceback (most recent call last):\r\n File \"<frozen runpy>\", line 198, in _run_module_as_main\r\n File \"<frozen runpy>\", line 88, in _run_code\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\__main__.py\", line 3, in <module>\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\Word2PDF_Python.py\", line 127, in cli\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\Word2PDF_Python.py\", line 76, in convert\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\Word2PDF_Python.py\", line 26, in windows\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\packages\\comtypes\\client\\__init__.py\", line 273, in CreateObject\r\n File \"D:\\Word2PDF-Service\\WindowsService1\\bin\\Debug\\Word2PDF\\packages\\comtypes\\_post_coinit\\misc.py\", line 149, in CoCreateInstance\r\n File \"_ctypes/callproc.c\", line 1008, in GetResult\r\nPermissionError: [WinError -2147024891] Access is denied