简体   繁体   English

通过VBS + python覆盖excel文件

[英]Overwrite excel file via VBS + python

I have a function that should create a VBScript and save (overwrite) the Excel file, specified in this script with a password on modifying.我有一个函数应该创建一个 VBScript 并保存(覆盖)Excel 文件,该文件在此脚本中指定,并在修改时使用密码。

The function:功能:

    def setPassword(self, excel_file_path, password):
    """Locks excel file with password. Modification allowed only when password is entered"""

    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(FileName:="{excel_file_path}", ReadOnly:=False, Notify:=False)

    excel_object.DisplayAlerts = False
    excel_object.Visible = False

    workbook.SaveAs "{excel_file_path}",,, "{password}"

    excel_object.Application.Quit
    """

    # write
    vbs_script_path = excel_file_path.parent.joinpath("set_password.vbs")
    with open(vbs_script_path, "w") as file:
        file.write(vbs_script)

    # execute
    result = subprocess.Popen(['cscript.exe', str(vbs_script_path)], creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

 
    output, err = result.communicate()
    res = {
        "result": output.decode(encoding='cp866'),
        "err": self._errMessage(err.decode(encoding='cp866'))
    }

    # remove
    vbs_script_path.unlink()

    return res

The issue is that on my PC it does it correctly.问题是在我的 PC 上它正确执行。 However, if I run it on my work PC, the VBS throws an exception that it can't access the file when trying to save it.但是,如果我在我的工作 PC 上运行它,VBS 会在尝试保存文件时抛出一个无法访问该文件的异常。

If I use a different name to save the file with password, it works just fine.如果我使用不同的名称来保存带有密码的文件,它就可以正常工作。 Sure, I could save the file with a different name, delete the initial file and then rename the new file.当然,我可以用不同的名称保存文件,删除初始文件,然后重命名新文件。 But it is a duck tape and I want a good solution.但它是一个鸭带,我想要一个好的解决方案。

PS I saw information that it is impossible to overwrite the file this way. PS我看到信息说不可能以这种方式覆盖文件。 But since it works on one computer, I suppose there should be a way to make it work on another.但既然它可以在一台计算机上运行,​​我想应该有办法让它在另一台计算机上运行。

PPS Inserting this XlSaveConflictResolution argument into the save func didn't help as well PPS 将此XlSaveConflictResolution参数插入保存函数也没有帮助

What is wrong with this VBS and how to make it work properly?这个 VBS 有什么问题以及如何使它正常工作?

Solved it by saving the file under a new name, then deleting the initial file and renaming the new file with the initial name.通过以新名称保存文件,然后删除初始文件并使用初始名称重命名新文件来解决该问题。 For example, I have a file named foo.xlsx.例如,我有一个名为 foo.xlsx 的文件。 I run the script and save the processed file as foe.xlsx.我运行脚本并将处理后的文件保存为 foe.xlsx。 Then I delete foo.xlsx with os.然后我用 os 删除 foo.xlsx。 os.unlink() . os.unlink() Finally, I rename the foe.xlsx back to foo.xlsx with os.rename()最后,我使用os.rename()将 foe.xlsx 重命名回 foo.xlsx

Hope one day this helps someone希望有一天这可以帮助某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM