Excel 中的注释提供了一种向数据添加上下文、说明或注释的便捷方法。在与他人合作或稍后审查我们自己的工作时,它们可能会有所帮助。了解如何有效地插入和管理评论可以简化我们的工作流程并改善我们团队内部的沟通。
为了在 Python 的 Excel 中插入和管理注释,本博客使用了一个名为 Spire.XLS for Python 的第三方库。
Spire.XLS for Python 是一个 Excel 库,用于在 Python 应用程序中创建、读取、编辑和转换 Excel 文件。该库与许多电子表格格式兼容,包括 XLS、XLSX、XLSB、XLSM、ODS 等。此外,它还提供了将 Excel 文件转换为其他文件格式的能力,例如 PDF、HTML、CSV、文本、图像、XML、SVG、ODS、PostScript 和 XPS。
我们可以通过在终端窗口中运行以下命令从 pypi 安装 Spire.XLS for Python:
pip install Spire.Xls有关安装的更多详细信息,请查看此官方文档:如何在 VS Code 中安装 Spire.XLS for Python。
使用 Python 向 Excel 添加注释在 Spire.XLS 中,单元格注释由 ExcelComment 类的对象表示。我们可以使用 CellRange 类的 AddComment 方法向单元格添加注释,并通过 ExcelComment 类的 Text 属性设置注释的文本。
下面的代码显示了如何为单元格添加注释并设置注释文本:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("CommentExample.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# Add a comment to cell "B1"comment = sheet.Range["B1"].AddComment()# Set comment textcomment.Text = "This is a comment"# Save the modified workbookworkbook.SaveToFile("AddComment.xlsx", ExcelVersion.Version2016)workbook.Dispose()
在 Python 中向 Excel 单元格添加注释
使用 Python 向 Excel 添加带有格式的注释应用格式可以提高评论的可读性和视觉吸引力。
Spire.XLS 提供了两种添加格式化注释的方法。第一种方法使用 ExcelComment 类的 RichText 属性,该属性允许将 RTF 格式应用于注释,例如自定义字体、颜色和样式。第二种方法利用 ExcelComment 类的 HtmlString 属性,使我们能够使用 HTML 样式的文本创建带有格式的注释。
下面的代码显示了如何添加注释和应用富文本格式:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("CommentExample.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# Set the author and content for the commentauthor = "Michael"text = "IT expenses in Q3 were higher than expected due to software license renewals"# Add a commentcomment = sheet.Range["D4"].AddComment()comment.Text = author + ":\n" + textcomment.AutoSize = True# Format the authorfont = workbook.CreateFont()font.FontName = "Calibri"font.KnownColor = ExcelColors.Blackfont.IsBold = Truecomment.RichText.SetFont(0, len(author), font)# Format the comment textfont = workbook.CreateFont()font.FontName = "Calibri"font.Size = 10.0font.KnownColor = ExcelColors.Redcomment.RichText.SetFont(len(author) + 1, len(author)+ 1 + len(text), font)# Save the modified workbookworkbook.SaveToFile("AddRichTextComment.xlsx", ExcelVersion.Version2016)workbook.Dispose()下面的代码显示了如何使用 HTML 样式的文本创建带有格式的注释:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("CommentExample.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# Add a commentcomment = sheet.Range["D4"].AddComment()comment.HtmlString = "<span style='font-family: Calibri; font-size: 11pt;'><b>Michael:</b></span><br><span style='font-family: Calibri; font-size: 10pt; color: red;'>IT expenses in Q3 were higher than expected due to software license renewals.</span>"comment.AutoSize = True# Save the modified workbookworkbook.SaveToFile("AddHtmlComment.xlsx", ExcelVersion.Version2016)workbook.Dispose()
在 Python 中向 Excel 添加带有格式的注释
使用 Python 在 Excel 中设置注释的背景颜色为注释添加背景颜色有助于使其在视觉上更加突出,并将其与工作表中的其他内容区分开来。
我们可以使用 ExcelComment 类的 Fill 属性向注释添加背景色。
下面的代码显示了如何在 Excel 中为评论设置背景颜色:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("CommentExample.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# Add a comment to cell "B1"comment = sheet.Range["B1"].AddComment()comment.Text = "This is a comment"# Set the background color for the commentcomment.Fill.FillType = ShapeFillType.SolidColorcomment.Fill.ForeColor = Color.get_SkyBlue()# Save the modified workbookworkbook.SaveToFile("SetCommentBackColor.xlsx", ExcelVersion.Version2016)workbook.Dispose()
在 Python 中为 Excel 注释添加背景颜色
使用 Python 在 Excel 的注释中插入图像当我们需要将视觉信息直接嵌入到单元格注释中时,插入图像非常有用。
Spire.XLS 允许使用 ExcelComment 类的 Fill 属性将图像添加到注释中。
下面的代码显示了如何在 Excel 的评论中插入图像:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("CommentExample.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# Add a comment to cell "B1"comment = sheet.Range["B1"].AddComment()# Insert an image in the commentimage = Stream("logo.png")comment.Fill.CustomPicture(image, "logo.png")# Save the modified workbookworkbook.SaveToFile("InsertImageInComment.xlsx", ExcelVersion.Version2016)workbook.Dispose()
在 Excel 中插入图像Python 中的注释
使用 Python 在 Excel 中显示或隐藏注释在 Excel 中,当我们将鼠标悬停在单元格上时,通常会显示注释。但是,我们可能希望让它们始终可见,尤其是在提供重要信息或反馈时。
我们可以使用 ExcelComment 类的 IsVisible 属性切换注释的可见性。
下面的代码显示了如何在 Excel 中显示或隐藏评论:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("AddComment.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# Show the first commentsheet.Comments[0].IsVisible = True# # Or hide the first comment# sheet.Comments[0].IsVisible = False# Save the modified workbookworkbook.SaveToFile("ShowOrHideComments.xlsx", ExcelVersion.Version2016)workbook.Dispose()使用 Python 在 Excel 中调整注释大小注释的默认大小可能并不总是适合内容,尤其是在注释包含大量文本时。在这种情况下,我们可以使用 ExcelComment 类的 Width 和 Height 属性调整注释框的大小。
下面的代码显示了如何在 Excel 工作表中调整注释的大小:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("AddComment.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# Change the width and height of the comment boxsheet.Comments[0].Width = 300sheet.Comments[0].Height = 150# Save the modified workbookworkbook.SaveToFile("ResizeComment.xlsx", ExcelVersion.Version2016)workbook.Dispose()使用 Python 从 Excel 中提取评论有时,我们可能希望提取电子表格中的注释,以创建摘要或查看电子表格外部的反馈。
要从工作表中提取注释,我们可以遍历 Comments 集合,检索注释文本,并将它们保存到文本文件中。
下面的代码显示了如何从 Excel 工作表中提取注释:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("AddComment.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# Open a text file to save the commentswith open("ExtractedComments.txt", "w", encoding="utf-8") as file: for comment in sheet.Comments: # Write each comment's text to the file file.write(comment.Text + "\n")workbook.Dispose()使用 Python 从 Excel 中删除注释删除注释对于清理过时或不相关的注释至关重要,尤其是在共享工作簿中。使用 Spire.XLS,我们可以从 Excel 工作表中删除特定注释或所有注释。
下面的代码显示了如何从 Excel 工作表中删除注释:
from spire.xls import *# Open an Excel workbookworkbook = Workbook()workbook.LoadFromFile("AddComment.xlsx")# Get the first worksheetsheet = workbook.Worksheets[0]# # Remove comment from a specific cell# sheet.Range["C2"].Comment.Remove()# Remove all commentsfor i in range(len(sheet.Comments) - 1, -1, -1): comment = sheet.Comments[i].Remove()# Save the modified workbookworkbook.SaveToFile("RemoveComments.xlsx", ExcelVersion.Version2016)workbook.Dispose()