[英]openpyxl 'Worksheet' object has no attribute 'write'(python)
Sorry for my English. 对不起我的英语不好。 I need to open a xlsx document and write in the last position new values.
我需要打开一个xlsx文档,并在最后一个位置写入新值。 But I don't understand how to do it.
但是我不知道该怎么做。 My algorithm works like this:
我的算法是这样的:
l_workbook = load_workbook(old_log_tmp_path)
l_workbook = load_workbook(old_log_tmp_path)
Get all value from there 从那里获取所有价值
Code: 码:
def iter_rows(ws): for row in ws.iter_rows(): yield [cell for cell in row]
Create new xlsm file 创建新的xlsm文件
Code: 码:
workbook = xlsxwriter.Workbook(tf.name) worksheet = workbook.add_worksheet()
Copy all values from l_workbook
to workbook -> worksheet
将所有值从
l_workbook
复制到workbook -> worksheet
But I think it is not right, I think they exist in a simple way. 但是我认为这是不对的,我认为它们以简单的方式存在。 Like this:
像这样:
l_workbook = load_workbook('EX2cRqM7xi1D.xlsx')
sheet = l_workbook.get_sheet_names()[0]
worksheet = l_workbook.get_sheet_by_name(sheet)
worksheet.write(1, 1, "TEST")
Running that script gave me the error below: 运行该脚本给我以下错误:
AttributeError: 'Worksheet' object has no attribute 'write'
My question is: How can I open xlsm file and add new values to it (using openpyxl)? 我的问题是:如何打开xlsm文件并向其中添加新值(使用openpyxl)?
UPD: UPD:
i try this code, but not work 我尝试此代码,但不起作用
import openpyxl
workbook = openpyxl.load_workbook('tmp3by148hj.xlsx')
ws = workbook.worksheets[0]
ws.cell(row=1, column=1).value = 'TEST'
You need to write into a cell: 您需要写入一个单元格:
worksheet.cell(row=1, column=1).value = 'TEST'
and finally save your changes: 最后保存您的更改:
workbook.save('tmp3by148hj.xlsx')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.