使用 Python 2.7 向 Excel 单元格添加注释
Add comment to Excel cell with Python 2.7
我正在使用 Python 2.7 并尝试执行 here 的操作(使用 Python 向 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
但这是否会在 "A1" 单元格上创建我的 xlsx 文件而不注释,这是否告诉我 "TypeError : expected type 'unicode' " 和 "raise TypeError('expected ' + str(expected_type))".
你能帮我解决这个问题吗?谢谢
N.B。 : 我也试过 但它说 "no attribute AddComment"...
根据错误消息,听起来 Comment
期望它的参数是 Unicode,但你给它的是 8 位字符串。尝试给它 Unicode 字符串。
comment = Comment(u'This is the comment text', u'Comment Author')
如果您正在考虑 "but why do I need to prefix my literals with u
when they don't do that in the examples in the documentation?",那么这些示例很可能使用的是 Python 3,其中未加前缀的字符串文字被解释为 Unicode。
关于"which of these is correct?"的问题:
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
赋一个值,然后再给它赋第二个值,这是相当荒谬的;它不会导致这两个值以任何有趣的方式相关。第二个更有可能真正对您的工作表进行更改。
#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:$A'})
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()
我正在使用 Python 2.7 并尝试执行 here 的操作(使用 Python 向 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
但这是否会在 "A1" 单元格上创建我的 xlsx 文件而不注释,这是否告诉我 "TypeError : expected type 'unicode' " 和 "raise TypeError('expected ' + str(expected_type))".
你能帮我解决这个问题吗?谢谢
N.B。 : 我也试过
根据错误消息,听起来 Comment
期望它的参数是 Unicode,但你给它的是 8 位字符串。尝试给它 Unicode 字符串。
comment = Comment(u'This is the comment text', u'Comment Author')
如果您正在考虑 "but why do I need to prefix my literals with u
when they don't do that in the examples in the documentation?",那么这些示例很可能使用的是 Python 3,其中未加前缀的字符串文字被解释为 Unicode。
关于"which of these is correct?"的问题:
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
赋一个值,然后再给它赋第二个值,这是相当荒谬的;它不会导致这两个值以任何有趣的方式相关。第二个更有可能真正对您的工作表进行更改。
#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:$A'})
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()