Skip to content

get and set Sensitivity Labels in Docx file #1232

@msuguino

Description

@msuguino

Companies often require that sensitivity labels are set for Microsoft Office files such as docx and xlsx.

I've used python-docx combined with win32com (pywin32) to get the label from a file which had the label properly set, and then, set the same label in a new document to be read by python-docx.

To get the sensitivity label:
myWord.ActiveDocument.SensitivityLabel.GetLabel().LabelId

To get the sensitivity label:
myWord.ActiveDocument.SensitivityLabel.GetLabel().LabelName

To get the assignment method:
myWord.ActiveDocument.SensitivityLabel.GetLabel().AssignmentMethod

To get the justification:
myWord.ActiveDocument.SensitivityLabel.GetLabel().Justification


I used the code below to set the label in a new document:

# Opening a MS Word file in pywin32 to set sensitivity label
myWord = Dispatch('Word.Application', pythoncom.CoInitialize())
myWord.Visible = 0
myWord.Documents.Open(src_absolute)

# Set Label
label_info = myWord.ActiveDocument.SensitivityLabel.CreateLabelInfo()
label_info.AssignmentMethod = 2
label_info.Justification = 'init'
label_info.LabelId = '123123123-123123123-ajskldfj123'
label_info.LabelName = 'Confidential'
myWord.ActiveDocument.SensitivityLabel.SetLabel(label_info, label_info)

# Close Word Application
myWord.ActiveDocument.Save()
myWord.Quit()

The problem lies in that linux servers often do not support microsoft office. The win32com solution is also slow, as it has to open the docx file, change settings, save it, and then close it.

By checking the Docx file and unzipping it, I found out that inside the folder "docProps" there is a file called "custom.xml" which stores the sensitivity label information.

Is it possible to support getting and setting sensitivity labels in python-docx?

Best regards

Marcos

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions