0

I have a function that convert the generated .docx file to .PDF and save it to the directory, after the save I want to print it to the printer.

I am having problem with the fileName, I am not able to read the directory.

pywintypes.error: (2, 'ShellExecute', 'The system cannot find the file specified.')

I am using win32api.ShellExecute() for printing.

doc.save("./cr/generated_doc"+itemid1+".docx")
pythoncom.CoInitialize()
convert("./cr/generated_doc"+itemid1+".docx",
        "./cr/pdf/label"+itemid1+".pdf")

printername = "Microsoft Print to PDF"
fileName = str(r"./cr/pdf/label"+itemid1+".pdf")
win32api.ShellExecute(0, "printto", filename, f'"{printername}"', ".", 0)

os.remove("./cr/generated_doc"+itemid1+".docx")
os.remove(fullnm)
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
Jimi
  • 13
  • 6
  • Have you tried using an absolute path? – user3738870 Aug 24 '22 at 14:40
  • Why would having a different file name each time prevent you from using an absolute path? – user3738870 Aug 24 '22 at 14:47
  • @user3738870 you mean trying like this? printername = "Microsoft Print to PDF" filename = './cr/pdf/label"+itemid1+".pdf' abs_path = os.path.abspath(filename) win32api.ShellExecute(0, "printto", abs_path, f'"{printername}"', ".", 0) – Jimi Aug 24 '22 at 14:56
  • @user3738870 with the path like this it works, filename = 'C:\Users\jimi\Desktop\folder\test.pdf' – Jimi Aug 24 '22 at 14:59
  • Oh I see, then it's a Windows problem. You have to use \ instead of /, see https://stackoverflow.com/questions/16010992/how-to-use-directory-separator-in-both-linux-and-windows-in-python for more details – user3738870 Aug 24 '22 at 15:00
  • @user3738870 still not working: filename = str(r'.\cr\pdf\label"+itemid1+".pdf') win32api.ShellExecute(0, "printto", filename, f'"{printername}"', ".", 0) – Jimi Aug 24 '22 at 15:12
  • @user3738870 pywintypes.error: (2, 'ShellExecute', 'The system cannot find the file specified.') – Jimi Aug 24 '22 at 15:13
  • What is `fullnm`? – Peter Wood Aug 24 '22 at 16:32
  • What's the difference between `filename` and `fileName`? – Peter Wood Aug 24 '22 at 16:34
  • For paths, use Python's pathlib, it will solve many problems portably. – Ulrich Eckhardt Aug 24 '22 at 16:40

1 Answers1

0

On Windows, directories are separated by \ in paths instead of /. In order to handle paths OS-independently, you have to use a Python helper function, os.path.join. Using this, your code should not encounter path-related errors:

doc_file_path = os.path.abspath(os.path.join("cr", "generated_doc"+itemid1+".docx"))
pdf_file_path = os.path.abspath(os.path.join("cr", "pdf", "label"+itemid1+".pdf"))
doc.save(doc_file_path)
pythoncom.CoInitialize()
convert(doc_file_path, pdf_file_path)

printername = "Microsoft Print to PDF"
win32api.ShellExecute(0, "printto", pdf_file_path, f'"{printername}"', ".", 0)

os.remove(doc_file_path)
os.remove(fullnm)
user3738870
  • 1,415
  • 2
  • 12
  • 24
  • I get an error: PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Jimi\\Desktop\\labelsnep\\cr\\generated_docAC0030.docx' – Jimi Aug 26 '22 at 07:15
  • That seems like a separate issue. I recommend opening a new question. – user3738870 Aug 26 '22 at 10:47