首页 > 解决方案 > Python:在 pdf 文件目录上运行打印文件应用程序时出现 pywintypes.error

问题描述

我是编程新手,我有以下脚本

import os
import tkinter as tk
import win32print
import win32com.shell.shell as shell
import win32api

root = tk.Tk()
root.title('Printing file app')
root.geometry('400x200')

check_var_list = []
file_list = []


def Print_act():
    def Print(file_link):
        print(file_link)
        win32print.SetDefaultPrinter('Printer_Name')
        win32api.ShellExecute(0, "print", file_link, None, '.', 0)

    for open_or_close, file_nam in zip(check_var_list, file_list):
        if open_or_close.get() == 1:
            file_link = os.path.join(t.get(), file_nam)
            os.startfile(file_link)
            Print(file_link)
        else:
            continue


def show_os(event):
    if not os.path.exists(t.get()):
        entry.destroy()
        label.destroy()
        la = tk.Label(root, text='Your file path is wrong or does not exist.')
        la.pack(pady=23)
        la.configure(font=('Century', 15, 'bold'))
        root.geometry('800x200')
    else:
        entry.destroy()
        label.destroy()

        b = tk.Button(root, text='Open', command=Print_act).pack()

        for roots, dirs, files in os.walk(t.get()):
            for sln, file_names in enumerate(files):
                file_list.append(file_names)

                check_var_list.append(tk.IntVar(value=1))
                ch = tk.Checkbutton(root, text=file_names, variable=check_var_list[sln])
                ch.pack()


t = tk.StringVar()
label = tk.Label(root, text='Enter file path here: ')
label.pack(padx=23, pady=23)
entry = tk.Entry(root, textvariable=t)
entry.pack()
root.bind('<Return>', show_os)
root.configure(bg='#856ff8')
root.mainloop()

该代码旨在执行以下步骤:

当我有一个.txt.docx文档时,所有这些都可以正常工作,但是当有一个包含 pdf 文件的目录时,此代码会打开第一个 pdf,然后引发以下错误

pywintypes.error: (31, 'ShellExecute', 'A device attached to the system is not functioning.')

我无法修复它,可能是因为我的每个 pdf 文件都使用网络浏览器打开。

我在 Python 3.8 上。

请帮忙!

标签: pythonpython-3.xtkinteroperating-systempywin32

解决方案


推荐阅读