最近我们的 IT 部门应用了一项政策,我们必须在 Excel 中指定敏感度标签。 这导致我用于生成个性化 Excel 文件的自动化 py 代码中断。
除了使用Excelwriter写入文件外,我还使用win32com密码保护随 secret 码的个人文件。每当 win32com 打开文件时,都会提示选择无法绕过的敏感度标签,因此无法继续进行。
有没有办法以编程方式默认选择“ secret ”作为敏感度标签?
以下是我如何生成文件的简要说明
while rc < idcount:
var_id= df['idnum'].iloc[rc]
var_password = df['password'].iloc[rc]
main_df = df[(df['idnum'] == var_id)]
wsname = 'testsheet'
staff_file = pd.ExcelWriter(dummyloc + 'Dummy.xlsx', engine='xlsxwriter')
main_df.to_excel(staff_file,wsname,startrow=4,index=False)
workbook = staff_file.book
workbook.set_vba_name('ThisWorkbook')
workbook.add_vba_project(dummyloc + './vbaProject.bin') #i added a macro for some internal controls.
staff_file.save()
staff_file.close()
excel = Dispatch('Excel.Application')
wb = excel.Workbooks.Open(dummyloc + 'Dummy.xlsx')
excel.Worksheets[wsname].Activate()
excel.ActiveSheet.Protect('ABCD',True ,True ,True ,False ,False , False , False , False , False , False , False ,False , True , True , False)
wb.SaveAs(Filename = dummyloc + 'Excel\\' + 'Dummy1',FileFormat= 52,Password=str(var_password))
wb.Close()
rc = rc+ 1
我也找到了一个建议的解决方案,但不知何故。 它应该执行一个 vba 宏来注入(inject)密码。然而,也许我没有正确编码,它没有改变任何东西
def set_password(excel_file_path, pw):
from pathlib import Path
excel_file_path = Path(excel_file_path)
vbs_script = \
f"""' Save with password required upon opening
Set excel_object = CreateObject("Excel.Application")
Set workbook = excel_object.Workbooks.Open("{excel_file_path}")
excel_object.DisplayAlerts = False
excel_object.Visible = False
workbook.SaveAs "{excel_file_path}",, "{pw}"
excel_object.Application.Quit
"""
# write
vbs_script_path = excel_file_path.parent.joinpath("set_pw.vbs")
with open(vbs_script_path, "w") as file:
file.write(vbs_script)
#execute
subprocess.call(['cscript.exe', str(vbs_script_path)])
# remove
vbs_script_path.unlink()
return None
最佳答案
刚刚在我的工作场所遇到了 excel 365 更新的类似问题。简而言之,我通过网上冲浪管理的是一个快速的 VBA 函数,它从一个已经标记的文件中获取敏感度标签,然后将其分配给我需要的所有文件。
请原谅我将 VBA 代码放在 Python 问题中,但由于您使用的是 win32com,因此改编应该非常简单。
这是我写的函数:
Sub put_label()
'Puts sensitivity labels copied from active workbook to a list of files.
Dim ex_lab 'To store the label object
Dim fs, f, archivos, curarch
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(Range("C2").Value2)
Set archivos = f.Files 'List of files to be labelled
'This line gets the label object from the already labelled file.
Set ex_lab = ActiveWorkbook.SensitivityLabel.GetLabel()
'The label is applied to all the files.
For Each curarch In archivos
Workbooks.Open curarch.path, False
'ActiveWorkbook is now the just opened workbook
ActiveWorkbook.SensitivityLabel.SetLabel ex_lab, ex_lab
ActiveWorkbook.Save
ActiveWorkbook.Close False
Next
MsgBox "Done"
End Sub
因此,根据您所拥有的,您可以使用 excel_object 并直接引用工作簿对象等,因为这些方法通常看起来与 VBA 中的方法相同。只是注意在 Python 中使用它时,所有的函数都带有 ()。例如,VBA 的:
ActiveWorkbook.Close False
应该在 Python 代码中是这样的:
excel_object.ActiveWorkbook.Close(False)
此外,Set 语句是 VBA 声明对象而不是内置变量的方式;在 Python 中不是必需的。
有时会出现一些区分大小写的问题,但我想您已经有通过 COM 对象从 Python 使用 VBA 的经验。
希望这会有所帮助,整个早上都在担心这个问题:)
关于python - 如何使用 Python 以编程方式设置 Excel 敏感度标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69625923/