【问题标题】:Add comment to Excel cell with Python 2.7使用 Python 2.7 向 Excel 单元格添加注释
【发布时间】:2019-03-24 08:27:56
【问题描述】:

我正在使用 Python 2.7,我尝试执行 here 的操作(使用 Python 将 cmets 添加到 excel 文件工作表中),但这不起作用...

这是我的代码:

import os, sys, shutil, time, openpyxl
from openpyxl import Workbook
from openpyxl.comments import Comment
...
path = 'K:/....../data.xlsx'
wb = Workbook()
ws = wb.active
...
comment = ws["A1"].comment
comment = Comment('This is the comment text', 'Comment Author')
...
wb.save(path)

我也试过了:

comment = Comment('This is the comment text', 'Comment Author')
ws["A1"].comment = comment

但这是否会创建我的 xlsx 文件而没有对“A1”单元格进行注释,这是否告诉我“TypeError:预期类型 'unicode'”和“引发 TypeError('expected ' + str(expected_type))”。

你能帮我解决这个问题吗?谢谢

注意:我也试过this,但它说“没有属性AddComment”......

【问题讨论】:

    标签: python excel python-2.7 openpyxl


    【解决方案1】:

    根据错误消息,听起来Comment 期望它的参数是 Unicode,但你给它的是 8 位字符串。尝试给它 Unicode 字符串。

    comment = Comment(u'This is the comment text', u'Comment Author')
    

    如果你在想“但是为什么我需要在我的文字前面加上 u,而他们在 documentation 的示例中没有这样做?”,那么这些示例很可能使用 Python 3,其中无前缀的字符串文字被解释为 Unicode。


    至于“哪一个是正确的?”的问题:

    comment = ws["A1"].comment
    comment = Comment(u'This is the comment text', u'Comment Author')
    

    comment = Comment(u'This is the comment text', u'Comment Author')
    ws["A1"].comment = comment
    

    第二个对我来说更有意义。给变量comment 赋值,然后再给它赋值,这是相当荒谬的;它不会导致两个值以任何有趣的方式相关。第二个更有可能实际更改您的工作表。

    【讨论】:

    • 非常感谢,这正好解决了我的问题!非常感谢您向我解释背后的原因。我对 Python 2 和 3 之间的区别(在编码方式上)非常缺乏了解,我被敦促使用 Python 2(不允许使用 Python 3),所以有时当我找到解决方案时很难一个不起作用的问题,因为它被认为在 3 上运行!再次感谢您。
    【解决方案2】:
    #full script on xlsxwriter
    
    import xlsxwriter
    
    workbook = xlsxwriter.Workbook('chart_styles.xlsx')
    
    # Show the styles for all of these chart types.
    chart_types = ['column', 'area', 'line', 'pie']
    
    for chart_type in chart_types:
    
        # Add a worksheet for each chart type.
        worksheet = workbook.add_worksheet(chart_type.title())
        worksheet.set_zoom(30)
        style_number = 1
    
        # Create 48 charts, each with a different style.
        for row_num in range(0, 90, 15):
            for col_num in range(0, 64, 8):
    
                chart = workbook.add_chart({'type': chart_type})
                chart.add_series({'values': '=Data!$A$1:$A$6'})
                chart.set_title ({'name': 'Style %d' % style_number})
                chart.set_legend({'none': True})
                chart.set_style(style_number)
    
                worksheet.insert_chart(row_num, col_num , chart)
                style_number += 1
    
    # Create a worksheet with data for the charts.
    data_worksheet = workbook.add_worksheet('Data')
    data = [10, 40, 50, 20, 10, 50]
    data_worksheet.write_column('A1', data)
    data_worksheet.hide()
    
    workbook.close()
    

    【讨论】:

    • 希望有帮助,它在 xlsxwriter 上的完整脚本
    猜你喜欢
    • 2018-01-17
    • 2018-04-25
    • 2018-04-23
    • 2020-01-29
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    相关资源
    最近更新 更多