-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_scanners.py
48 lines (40 loc) · 1.57 KB
/
list_scanners.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import twain
def list_scanners():
try:
# Initialize TWAIN source manager
sm = twain.SourceManager(0)
# List available sources
sources = sm.GetSourceList()
if not sources:
print("No scanners connected.")
return
connected_scanners = []
for source_name in sources:
source = None
try:
# Attempt to open the source
source = sm.OpenSource(source_name)
if source:
# If source is successfully opened, add it to the list
connected_scanners.append(source_name)
except Exception as e:
print(f"Error with source '{source_name}': {e}")
finally:
# Ensure source is properly managed
if source:
try:
# Check if CloseSource is available
if hasattr(source, 'CloseSource'):
source.CloseSource()
else:
print(f"Source '{source_name}' does not support CloseSource.")
except Exception as e:
print(f"Error closing source '{source_name}': {e}")
if connected_scanners:
print(f"Connected scanners: {', '.join(connected_scanners)}")
else:
print("No scanners connected.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == '__main__':
list_scanners()