How to write excel comments using python?

I am a Python programmer and I need to add notes in bulk to an Excel spreadsheet. Does anyone know how easy / practical it is to use a Python script? (may be a Java script if it has examples).

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

I tried the xlwt module for python and was looking for something to do this with csv, but to no avail.

+4
source share
1 answer

Here is the python code that the pywin32 package on Windows uses to run Excel, open the 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() 

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

+1
source

Source: https://habr.com/ru/post/1401710/


All Articles