1

我希望在工作中自动化一些任务。其中之一是将 power point 文件合并并转换为 PDF。我是一个新手(我刚刚完成了 Magus Heitland 的 Python 入门),所以我不完全确定我具体要问什么。

在windows上,可以选择多个文件,右键单击,然后选择合并为adobe PDF。我已经弄清楚了要转换的文件的“分组”(我遍历目录并根据文件名将文件嵌套在列表中),但我不确定如何进行下一步(右键单击/组合命令)。

谷歌搜索让我找到了诸如 win32api、pywinauto 和 ctypes 之类的东西。但是当我阅读他们所做的事情时,我的新手让我不知道我需要哪个工具。

有人可以提出一些好的资源或提示吗?

4

3 回答 3

3

这个问题没有简单的答案。在 Windows 中,上下文菜单项是由上下文菜单外壳扩展创建的,它是外壳扩展的一种,是在 Windows 资源管理器中注册的 COM 组件。要在一组文件上调用任意上下文菜单条目,必须执行以下操作:

  1. 获取包含文件的 shell 文件夹的IShellFolder接口。
  2. 使用IShellFolder::GetUIObjectOf获取该 shell 文件夹的上下文菜单处理程序对象。将一个 ITEMIDLIST 传递给它,其中包含所选文件的列表和riid= IContextMenu
  3. 调用IContextMenu::InvokeCommand以对文件组执行所需的命令。

All this can be implemented in Python (via ctypes or win32com), but I think it's better to start with a language that supports COM directly (such as C++) and translate to Python once you get it working... or use a different approach.


EDIT If you have total control of the execution environment, it may be good enough to simulate user input (keyboard and mouse). It is much easier to do in Python. You will need just one system function: SendInput. Quick googling revealed an example of using SendInput via ctypes here.

于 2010-12-22T10:48:01.753 回答
1

当您单击 时,您想准确查找运行Combine的等效命令行,假设它是c:\program files\adobe\combiner .... 您想要构建此字符串(将文件列表作为...)并执行它。子进程模块中的Popen将执行此操作。

import subprocess
cmd = [r"c:\program files\adobe\combiner"]
cmd.extend(file_list)
p = subprocess.Popen(cmd)

不过看看那个链接,Popen 需要很多参数,这只是最简单的形式。不知道参数是否必须在列表中,但它们在我见过的示例中。

于 2010-12-22T07:58:13.373 回答
1

If you are just looking for a simple solution, you can customize a a "Send To..." right click shortcut that will invoke your python script with the command line arguments of the files to convert.

To add a Send To shortcut, it depends on if you are using Windows 7 or a 32 bit prior version.There are instructions HERE to add a custom Send To... shortcut. You could send all the files to a single folder and invoke your python script or invoke it with the selected files as command line arguments.

Send To Toys works in 64 bit. With Send To Toys you could have the your python script and all the shell escaped file names placed on the clipboard when you right click, then paste the constructed command into the shell. I use this method a lot because it only takes seconds to construct.

You can also add a right click shortcut by tweeking the registry to invoke a shell command (your python script) with the selected files as targets. You can either manually tweek the registry or use something like RightClick Extender.

A bit more work, you can write a custom ContextMenu Class form and respond to the form in a .NET language. This would include C# or Ironpython.

If you really want a Pro kind of implementation, you could modify the source code of the new open source version of Image Resizer. The c++ source the context menu handler is here. But these last two suggestions seem like way more work than needed...

于 2010-12-22T14:07:05.913 回答