使用Python操纵Office, 添加Outlook的联系人

使用的是Windows Mobile 的手机。 2B的Windows同步软件缺省会用PC上的覆盖手机上的内容,即使PC上一条记录也没有,也会把手机上面的记录全部清空。

而且发现不同手机操作系统的手机通讯录无法自动转换。--- 就TMD的姓名,手机号两个字段。就不能创建一个统一的格式?

唯一统一的格式vCard. 2B 的Office只能导入一条记录,哪怕一个vCard文件里面有再多的记录。在经历恶心加愤怒(fengnu,不是fengnong, 怒字上奴下心)。

幸亏咱们是程序员,自己动手丰衣足食。

用熟悉的Python和不熟悉的Office COM 接口:

http://www.the-generalist.com/component/content/article/46-programming/105-adding-a-contact-to-outlook-using-python.html

import win32com.client

#Connect to Outlook
Outlook = win32com.client.Dispatch("Outlook.Application")

#Create a new Contact and save it
NewContact = Outlook.CreateItem(win32com.client.constants.olContactItem)

NewContact.FullName = 'NewContact'

NewContact. MobileTelephoneNumber = '13871609999'

NewContact.Save()

这个代码够简单,但是如果成功运行需要如下操作:

First, download and install Python (http://www.python.org/).  I recommend Python 2.7.  It's been around longer then Python 3, has more support, and I know it works.

Second, you need to download and install the Python Extension for Windows (http://sourceforge.net/projects/pywin32/).  Make sure you download the version that matches your version of Python.

Third, you need to run makepy.py.  I normally just do a search in my Python directory for the file.  Running it will bring up a window with a selection box.  Choose "Microsoft Excel 14.0 Object Library" or "Microsoft Office 14.0 Object Library".  Either one seems to work for me.  The version number may be different depending on the version of Microsoft Office you are using.

Now you're ready to go.  The next article will give an example of a basic Python Excel script.

前两步安装ActivePython就OK ,重要的是第三部,我查找发现makepy.py在我的C:\Python27\Lib\site-packages\win32com\client目录。

如果需要添加其他字段参考:

http://msdn.microsoft.com/en-us/library/ff868407.aspx

参考资料:

http://www.cnblogs.com/myamanda/articles/1744631.html

http://www.the-generalist.com/component/content/article/46.html

你可能感兴趣的:(python,添加,outlook,联系人)