首页 > 解决方案 > How to communicate with Excel files via subprocess using python?

问题描述

How do I change cells in an excel file with subprocess? So far, my code is:

import subprocess
ob =subprocess.Popen(['*Path to excel file*'], shell=True)
ob.communicate('H2=test')

But, it doesn't do anything!

Any help would be appreciated

标签: pythonexcelsubprocessoffice365

解决方案


我想也许你在这里很难理解子流程模块的实际作用。

当您在其中使用shell=True参数时,就像您打开终端(或 Windows 中的 cmd)并输入该参数一样。

如果您在 Windows 上,请尝试运行subprocess.call('dir', shell=True),看看会发生什么。您应该会看到显示的当前目录的内容。如果你在 mac/linux 上运行subprocess.call('ls', shell=True),它应该做同样的事情。

因此,对于您目前的任务,这不是正确的工具。但是,此模块对于其他任务可能非常有用。

就个人而言,我喜欢结合使用 openpyxl 和 Pandas 将 Excel 工作表读取到 DataFrame 并在 python 内部对其进行操作。Pandas 有一个很好的read_excel功能,文档在这里


推荐阅读