英文:
Open an Excel file as Read Only using os.startfile() with Python
问题
I made a tool for my colleagues to open Excel files we often use. Sometimes a colleague only needs to open it as Read Only. If I use os.startfile() and nobody else has opened the file, you will open the file as Writeable but the next person will only be able to open the file as Read Only. I want them to have an option to open the file as Read Only, no matter if it is already opened or not. I know Excel has an option to open a file as read only. Maybe it is possible to reach this option with Python?
我为同事制作了一个工具,用来打开我们经常使用的Excel文件。有时候,同事只需要以只读方式打开它。如果我使用os.startfile(),并且没有其他人打开该文件,那么你会以可写方式打开文件,但下一个人只能以只读方式打开文件。我希望他们有一个选项可以将文件以只读方式打开,无论是否已经打开。我知道Excel有一个选项可以将文件以只读方式打开。也许可以用Python达到这个目标?
I have Googled but the most things I find is about reading excel files in openpyxl or xlwings. But did not find anything about using os.startfile() to open it as Read Only. Can someone please guide me to the correct solution?
我已经使用谷歌搜索过,但大多数搜索结果都是关于在openpyxl或xlwings中读取Excel文件的内容。但我没有找到关于如何使用os.startfile()以只读方式打开文件的信息。有人可以指导我找到正确的解决方案吗?
英文:
I made a tool for my colleagues to open Excel files we often use. Sometimes a colleague only needs to open it as Read Only. If I use os.startfile() and nobody else has opened the file, you will open the file as Writeable but the next person will only be able to open the file as Read Only. I want them to have an option to open the file as Read Only, no matter if it is already opend or not. I know Excel has an option to open a file as read only. Maybe it is possible to reach this option with Python?
I have Googled but the most things I find is about reading excel files in openpyxl or xlwings. But did not find anything about using os.startfile() to open it as Read Only.
Can someone please guide me to the correct solution?
答案1
得分: 0
Excel将以只读模式启动,使用/r
开关,例如 excel.exe /r "c:\My Folder\book1.xlsx"
不要将电子表格文件名作为os.startfile
的第一个参数传递,而是传递Excel
,然后是正确的参数,使其以只读模式运行,然后是用双引号括起来的文件名。
os.startfile('Excel', arguments='/r "C:\spreadsheet file 1.xlsx"')
这与在Windows命令提示符中输入start Excel /r "C:\spreadsheet file 1.xlsx"
相同。
或者,您可以手动获取Excel的路径,然后使用winreg
和subprocess.Popen
运行它,带有正确的参数:
# 步骤1 - 获取EXCEL.EXE的路径(第一种方法)
# 这种方法只查询一次注册表,对我来说返回
# "C:\Program Files\Microsoft Office\Office15\EXCEL.EXE" "%1"
# 但我们只需要没有引号的路径
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"Applications\EXCEL.EXE\shell\open\command")
excel_open_command = winreg.QueryValueEx(key, "")[0]
winreg.CloseKey(key)
end = excel_open_command.lower().rindex(".exe") + len(".exe")
excel_exe_path = excel_open_command[:end].replace('"', "")
# 步骤1 - 获取EXCEL.EXE的路径(第二种方法)
# 这种方法查询两次注册表,对我来说返回短名称
# C:\PROGRA~1\MICROS~2\Office15\EXCEL.EXE
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"Excel.Sheet\CLSID")
clsid = winreg.QueryValueEx(key, "")[0]
winreg.CloseKey(key)
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, rf"CLSID\{clsid}\LocalServer32")
excel_exe_path = winreg.QueryValueEx(key, "")[0]
winreg.CloseKey(key)
# 步骤2 - 使用只读开关运行Excel,后跟电子表格文件名
subprocess.Popen([excel_exe_path, "/r", r"C:\Spreadsheet.xlsx"])
英文:
Excel will launch with a spreadsheet as read-only with the /r
switch, e.g. excel.exe /r "c:\My Folder\book1.xlsx"
Instead of passing the filename of the spreadsheet as the first argument to os.startfile
, pass Excel
to it instead, and then the right arguments to make it read-only followed by the filename enclosed in double quotes.
os.startfile('Excel', arguments='/r "C:\spreadsheet file 1.xlsx"')
This is the same as entering start Excel /r "C:\spreadsheet file 1.xlsx"
into the Windows Command Prompt.
Alternatively, you could manually get the path of Excel and run it with the right arguments using winreg
and subprocess.Popen
:
# Step 1 - get the path of EXCEL.EXE (first method)
# This method queries the registry just once, which for me returns
# "C:\Program Files\Microsoft Office\Office15\EXCEL.EXE" "%1"
# But we only need the path without quotes
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"Applications\EXCEL.EXE\shell\open\command")
excel_open_command = winreg.QueryValueEx(key, "")[0]
winreg.CloseKey(key)
end = excel_open_command.lower().rindex(".exe") + len(".exe")
excel_exe_path = excel_open_command[:end].replace('"', "")
# Step 1 - get the path of EXCEL.EXE (second method)
# This method queries the registry twice, which for me returns the short name
# C:\PROGRA~1\MICROS~2\Office15\EXCEL.EXE
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"Excel.Sheet\CLSID")
clsid = winreg.QueryValueEx(key, "")[0]
winreg.CloseKey(key)
key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, rf"CLSID\{clsid}\LocalServer32")
excel_exe_path = winreg.QueryValueEx(key, "")[0]
winreg.CloseKey(key)
# Step 2 - run Excel with the read-only switch followed by the spreadsheet filename
subprocess.Popen([excel_exe_path, "/r", r"C:\Spreadsheet.xlsx"])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论