怎么python编程excel_如何使用python编写excel注释?

博主分享了一段Python代码,利用pywin32库在Windows环境下启动Excel,打开工作簿并为单元格A1创建注释。此代码适用于.xls、.xlsx和.ods文件,并能在Linux环境下运行,为批量处理Excel文件提供了方便。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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).

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).

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

解决方案

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'')

>>> sheet = wb.ActiveSheet

>>> sheet.Range("A1").AddComment()

>>> sheet.Range("A1").Comment.Visible = True

>>> sheet.Range("A1").Comment.Text("Hello World")

u'Hello World'

>>> wb.SaveAs(r'')

>>> wb.Close()

>>> xl.Quit()

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值