Creating Documents from Streams

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