python - 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 上。
请帮忙!
解决方案
推荐阅读
- node.js - How to get parameters from url received in headers?
- reactjs - 如何在根文件夹中添加一个 React App,在子文件夹中添加另一个?
- python - 在 python 文件中解压的值错误(预期为 3)
- php - 通过引用帖子 ID 在列中显示自定义帖子类型帖子标题
- java - 将 NaN 作为字符串输出为“nan”的双精度值
- typescript - 返回类型化索引的通用数组
- javascript - 将表单锁定在html css中后更改边框
- sql - “不一致的数据类型:预期的 %s 得到了 %s”
- javascript - 在反应本机中使用 ref 调用 child 中的函数
- mysql - Does MySQL JDBC version have to match the server