0

I am having trouble with converting docx to pdf. I was using 'from win32com import client' which works correctly when I run the program, but now that I have it being run with a windows task scheduler job, it gives me an error 'NoneType' object has no attribute 'SaveAs'.

I've been reading this may be due to the task scheduler not being able to open the Word application and run the functions correctly.

I am bummed as I feel this should be something fairly simple or already solved for but I can't find anything that can do it. I have tried from docx2pdf import convert but that doesn't work either as it requires the user to be logged in and Word to be there.

It won't work as an unattended script. I have tried pypandoc but am having trouble installing it as when I run it it says no pandoc was found (I already pip installed it..).

martineau
  • 119,623
  • 25
  • 170
  • 301
  • When you schedule tasks to run under the Windows task scheduler, you must pay close attention to what user "credentials" (log-in name) the task is run under. It's somewhat dated, but see this [answer of mine](https://stackoverflow.com/a/4209102/355230) to a possibly related question. – martineau Jun 09 '22 at 22:58
  • Thanks for the tip. It didn't work for me unfortunately. I have the python path in system variables. It fails at worddoc.SaveAs. This works when I run it through the .bat normally. Do you have a script where you call the env var explicitly? word = client.DispatchEx("Word.Application") print('here2') worddoc = word.Documents.Open(filePath, ReadOnly=1) print('here3') worddoc.SaveAs(endFilePath_pdf, FileFormat=17) print('here4') – Elliot Gitter Jun 10 '22 at 16:41
  • Have no idea what you mean about calling the env var explicitly. Regardless, besides env var differences, each user has different privileges, so make sure whatever user the task is being run under via the scheduler has ones sufficient for it to run the Word application. – martineau Jun 10 '22 at 17:26
  • 1
    Thanks, yea sorry I got confused with a rabbit hole I went down with the link you shared. I may see if I can run as admin and hope that works. – Elliot Gitter Jun 13 '22 at 15:53
  • @martineau I've narrowed it down a little bit more. I can run the script when I explicitly tell the path of the file in only 1 for loop. When I create 2 for loops (iterating through folder structures), I get the error. I am trying to figure out if it's a syntax issue because in the easy example, the file path is filepath = r'folder\subfolder\test.docx' and the SaveAs works.. I am close but loosing my mind. Also noticed that it was giving me 'C:\Windows\system32\test.docx' when I checked the abspath of what I was iterating over. It's very strange – Elliot Gitter Jun 16 '22 at 00:36
  • Since you haven't provided a [mre], I can only guess. That `filepath` is relative to the current working directory (cwd) which could very likely be different between when you run it manually and the task scheduler does it. If that's the issue, you can workaround it several different ways — but I'm won't be describing each of them here in a comment. Verify that's now the issue and post another question if you can't figure out how to solve the problem (and be sure to include sufficient code). – martineau Jun 16 '22 at 01:31
  • Hi @martineau I created a new question here: 'Python word.SaveAs() - 'NoneType' object has no attribute 'SaveAs' - When ran with Task Scheduler' https://stackoverflow.com/questions/72648610/python-word-saveas-nonetype-object-has-no-attribute-saveas-when-ran-wi Please see for more detailed information. Thanks for all your help. I think I'm close – Elliot Gitter Jun 16 '22 at 15:52

0 Answers0