【问题标题】:Open an .ods file in Libre Office via python通过 python 在 Libre Office 中打开一个 .ods 文件
【发布时间】:2021-10-12 13:07:45
【问题描述】:

请放轻松,我刚开始使用 Python。

我想制作一个程序来节省我花在文件上的时间(用于工作)。我可以通过以下方式打开 Libre Office Calc:

subprocess.call(['C:\Program Files\LibreOffice\program\scalc.exe'])

但这会打开一个新文件。如果我打开我想要处理的文件,Libre Office 会生成一个新窗口,其中包含一个我不知道 ID 的新句柄(因此我无法测量这个句柄的运行时间)。

有没有可能直接打开文件?或者在我可以输入链接的地方输入以打开它?

【问题讨论】:

  • 我可能有点飘了,但是在启动 libre office 时直接打开你的文件可以接受吗?如果是这样,您应该能够将文件作为参数传递。
  • @peyo 可以接受。我试图用subprocess.call(['C:\Users\M3nsch\Desktop\\a.ods]']) 直接调用它,但这只是让我明白:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 如何将它作为参数调用?
  • 使用子进程时,您应该将每个参数作为列表传递给您要调用的二进制文件,例如:subprocess.call(['C:\\Program Files\\LibreOffice\\program\\scalc.exe', 'C:\\Users\\M3nsch\\Desktop\\a.ods'])。在我的情况下,subprocess.call(['C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE', 'C:\\Users\\peyo\\Documents\\tests.xlsx']) 工作正常 :) 不要忘记两个路径中的双反斜杠 `\`。
  • 您的 unicode 错误是由 C:\Users... 中的 \U 引起的,python 将其解释为 unicode 字符(事实并非如此)。所以当使用\` it tells to use the special character `代替特殊字符\Users时。在我的示例中,\test 将被解释为<TAB>est,所以.. 对您想要实现的目标没有好处。
  • @peyo 感谢您的帮助,我找到了解决方法!

标签: python file subprocess


【解决方案1】:

好吧,我找到了一个解决方法: 我没有使用 Python 打开程序,而是让 pywinauto 列出所有程序,然后记下程序的名称:

while True:
    x=1
    while x <= 2:
        windows = Desktop(backend="uia").windows()
        print("If you do not find your program in the list write '@'")
        print("")
        for w in windows:
            if w.window_text()!="":
                print(str(w.window_text()))
        program_Open=str(input())
        if program_Open!="@":
            x=3

if w.window_text()!="":是删除后台程序引起的列表中的空行。

【讨论】:

    猜你喜欢
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多