【问题标题】:How to display and change SensitivityLabel for a Word document (desktop)?如何显示和更改 Word 文档(桌面)的 SensitivityLabel?
【发布时间】:2022-11-03 02:06:35
【问题描述】:

我想知道我当前 Word 文件的敏感度标签,用新值更改它并保存我的文件

我首先打开一个 Word 文件

    # Opening a MS Word file in pywin32
    from win32com.client import Dispatch
    myWord = Dispatch('Word.Application')
    myWord.Visible = 1
    myWord.Documents.Open("C:/./TEMP.docx")  # open file

    # SetLabel and GetLabel
    print(myWord.ActiveDocument.SensitivityLabel)
    print(myWord.ActiveDocument.SensitivityLabel.SetLabel)
    print(myWord.ActiveDocument.SensitivityLabel.GetLabel())

    # Create label info
    myLabelInfoNew = myWord.ActiveDocument.SensitivityLabel.CreateLabelInfo()

    # Close Word Application
    myWord.ActiveDocument.SaveAs("C:/./TEMP2.docx")
    myWord.Quit()

我该如何解决?

谢谢您的帮助

【问题讨论】:

  • 代码中断为这一行:myWord = Dispatch('Word.Application')。你能确认这也是你的情况吗?
  • 此行没有问题(错误地复制了两次)
  • 好吧,我没有消息,只有libre office,所以帮不上忙。希望其他人解决...

标签: python python-3.x ms-word win32com azure-information-protection


【解决方案1】:

这是我用来对 .xlsx 文档应用敏感度的代码。在获得公司敏感度 ID 的任何文档上运行第一个函数。然后使用适当的 ID 更新第二个函数中的字典。要在 .docx 上使用此功能,您需要更改以下代码中的一些内容。有关更改,请参阅以下项目。

  1. “Excel.Application”到“Doc​​ument.Application”
  2. .Workbooks.Open to Documents.Open
  3. .ActiveWorkbook 到 .ActivateDocument

    下面更新docx

    from win32com.client import Dispatch
    def get_lable(in_xlsx):
    
        """
        :param in_xlsx: Input file to attach sensitivity label
        """
        myxlsx = Dispatch('Excel.Application')
        myxlsx.Visible = 1
        myxlsx.Workbooks.Open(in_xlsx)
    
        # Get Label
        print(myxlsx.ActiveWorkbook.SensitivityLabel.GetLabel())
        myxlsx.Application.Quit()
        return str(label_id)
    
    def xlsx_sensitivity_label(in_xlsx, label='Internal'):
        """
        Update XLSX file with sensitivity label
        https://pythoninoffice.com/python-set-sensitivity-label-in-excel/
        :param in_xlsx: path of input .xlsx file to attach sensitivity label
        :param label: Accepted Labels: Public, Internal, Confidential, Restricted
        :return: Adds Microsoft Sensitivity label to spreadsheet
         """
        di_sensitivity = {'Public': 'xxxx-xxx-xxxxxxx-xxxx-xxx-xxx',
                          'Confidential': 'xxxx-xxx-xxxxxxx-xxxx-xxx-xxx',
                          'Internal': 'xxxx-xxx-xxxxxxx-xxxx-xxx-xxx',
                          'Restricted': 'xxxx-xxx-xxxxxxx-xxxx-xxx-xxx'}
    
        label_id = di_sensitivity[label]
        myxlsx = Dispatch('Excel.Application')
        myxlsx.Visible = 1
        myxlsx.Workbooks.Open(in_xlsx)
    
        # Set Label
        label_info = myxlsx.ActiveWorkbook.SensitivityLabel.CreateLabelInfo()
        label_info.AssignmentMethod = 2
        label_info.LabelId = label_id
        label_info.LabelName = label
        print(label_info.LabelName)
        myxlsx.ActiveWorkbook.SensitivityLabel.SetLabel(label_info,label_info)
        myxlsx.ActiveWorkbook.Save()
        myxlsx.Application.Quit()
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-11
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    相关资源
    最近更新 更多