0

Recently I use modules win32print and win32api to accomplish batch printing PDF files by python. When I select pdf files on my local computer, it did well. However, when I select pdf files on a shared folder on another computer(the folder path like "\\filepath"), The problem appeared, which showed "pywintypes. error: (2, 'ShellExecute', 'The system cannot find the file specified)". I do not know why.

error

# Batch print pdf files

import win32print
import win32api
import os

import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
 
def print_file(filename):
    open(filename,"r")
    win32api.ShellExecute(
        0,
        "print",
        filename,
        '/d:"%s"' % win32print.GetDefaultPrinter(),
        ".",
        0
    )
    
root = tk.Tk()
root.withdraw()

files_path = filedialog.askopenfilenames()
num = len(files_path)

if num == 0:
    messagebox.showinfo("Prompting","No files selected!")
else:
    msg = messagebox.askyesno('Prompting', 'Pring'+ str(num)+"files?")
    
    if msg:
        i = 0
        for file_path in files_path:
            if file_path.endswith("pdf"):
                print_file(file_path)
                i = i + 1
        messagebox.showinfo("Prompting","Done!\n" + "Totally printing"+str(i)+"files!")
    else:
        messagebox.showinfo("Prompting","Nothing done!")
Sam Lin
  • 5
  • 4

1 Answers1

0

probably you can use these:

import glob
path = r'./folder/folder/'
all_files = glob.glob(path + "/*.pdf")
files = all_files[:-1]
print("number of pdf files: ", len(files))
for filename in files:
   //open file
dimz
  • 179
  • 8
  • Thanks for your answer, I need to use filedialog.askopenfilenames() to get the files I need to print. so I do not know how to conbine glob to solve my need. – Sam Lin Jan 28 '22 at 13:57