如何使用python编写excel注释? [英] How to write excel comments using python?

查看:604
本文介绍了如何使用python编写excel注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名python程序员,我必须在excel电子表格中大量插入笔记。有没有人知道使用python脚本容易/实用的方法? (可能是一个java脚本,如果它有示例)。

I'm a python programmer and I have to insert notes in mass in an excel spreadsheet. Does anybody knows some way that's easy/practical using a python script? (could be a java script, if it had examples).

它可以在.xls,.xlsx或.ods上,并且可以在任何操作系统中(尽管我是一个Linux用户,所以如果我可以将我的解决方案移植到linux,那就更好了。)

It can be on a .xls, .xlsx or .ods, and can be in any OS (although I'm a linux user, so if I could have my solution portable to linux, it would be better).

我已经尝试了xlwt模块用于python并搜索了使用csv来做到这一点,但没有成功。

I've tried xlwt module for python and searched for someway to do this using csv, but no success.

推荐答案

这里有一些使用 pywin32 包启动Excel,打开电子表格并在单元格A1中创建注释:

Here's some python code that uses the pywin32 package on Windows to start Excel, open a spreadsheet and create a comment in cell A1:

>>> import win32com.client
>>> xl = win32com.client.Dispatch("Excel.Application")
>>> xl.Visible = 1
>>> wb = xl.Workbooks.Open(r'<full path of excel spreadsheet>')
>>> sheet = wb.ActiveSheet
>>> sheet.Range("A1").AddComment()
<COMObject AddComment>
>>> sheet.Range("A1").Comment.Visible = True
>>> sheet.Range("A1").Comment.Text("Hello World")
u'Hello World'
>>> wb.SaveAs(r'<full path of modified spreadsheet>')
>>> wb.Close()
>>> xl.Quit()

我刚在Windows 7上用Excel 2007使用python 2.7.2运行此代码它对我有用。

I just ran this code using python 2.7.2 on Windows 7 with Excel 2007 and it worked for me.

这篇关于如何使用python编写excel注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆