Creating Documents from Streams¶
Creating Documents from Streams¶
1import win32com.client
2ot = impact.gui.output_toolbox
3db = impact.active_database
4
5docs = db.documents
6
7
8def describe_doc(doc, method):
9 if not doc.isNone():
10 ot.add("Document '" + doc.name + "' successfully created with " + str(method))
11 ot.add("description: " + str(doc.description))
12 ot.add("Checkin-Comment: " + str(doc.comment))
13 ot.add("Added: " + str(doc.added_date_time) + " by " + str(doc.added_by.login_id))
14 ot.add("version: " + str(doc.version.version_as_string))
15 ot.add("Size: " + str(doc.size) + " bytes")
16 else:
17 ot.add("Unable to create new document with " + str(method))
18
19def read_stream(file):
20 stream = win32com.client.Dispatch("ADODB.Stream")
21
22 stream.open()
23 stream.type = 1 # ad_type_binary
24 stream.load_from_file(file)
25
26 _result = stream
27 return _result
28
29
30ot.clear()
31
32docs = db.documents
33
34if docs.isNone():
35 ot.add("No document support in the database")
36elif db.settings.other_documents:
37 ot.add("Other document support is enabled in this database")
38 ot.add("Major and minor versions support: " + str(db.settings.document_settings.supports_major_and_minor_versions))
39 ot.add(str(docs.total_count) + " documents currently in the database")
40
41 # using deprecated AddFromStream
42 doc_stream = read_stream(r"C:\Documents\ExampleDocument1.docx")
43 doc = docs.add_from_stream(doc_stream, "ExampleDocument1.docx", 0, "My First Document")
44
45 describe_doc(doc, "AddFromStream")
46
47 # using AddFromStream2 without values
48 doc_stream = read_stream(r"C:\Documents\ExampleDocument2.docx")
49 doc2 = docs.add_from_stream2(doc_stream, "ExampleDocument2.docx", 0, None, ipDocumentVersion.dvMajor, "My Second Document")
50
51 describe_doc(doc2, "AddFromStream2 (no values)")
52
53 # using AddFromStream2 with values
54 # first determine document type and columns for file
55 doc_type = db.settings.document_settings.mime_types.find_type_for_extension(r"C:\Documents\ExampleDocument3.docx")
56
57 ot.add("Document Type: " + str(doc_type.key))
58
59 # create some values and populate using ipDocumentKnownColumn values
60 columns = doc_type.columns
61 values = doc_type.create_column_values()
62
63 values.save(columns.known_item(ipDocumentKnownColumn.dkcName).name, "ExampleDocument3")
64 values.save(columns.known_item(ipDocumentKnownColumn.dkcDescription).name, "My Third Document")
65
66 doc_stream = read_stream(r"C:\Documents\ExampleDocument3.docx")
67 doc3 = docs.add_from_stream2(doc_stream, "ExampleDocument3.docx", 512, values, ipDocumentVersion.dvMinor, "My Third Document")
68
69 describe_doc(doc3, "AddFromStream2 (with values)")
70
71 # must reload IDocuments
72 docs2 = db.documents
73 ot.add(str(docs2.total_count) + " documents currently in the database")
74
75else:
76 ot.add("No other document support in the database")