简体   繁体   English

openpyxl'工作表'对象没有属性'写'(python)

[英]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: 我的算法是这样的:

  1. Open xlsx l_workbook = load_workbook(old_log_tmp_path) 打开xlsx l_workbook = load_workbook(old_log_tmp_path)
  2. Get all value from there 从那里获取所有价值

    Code: 码:

     def iter_rows(ws): for row in ws.iter_rows(): yield [cell for cell in row] 
  3. Create new xlsm file 创建新的xlsm文件

    Code: 码:

     workbook = xlsxwriter.Workbook(tf.name) worksheet = workbook.add_worksheet() 
  4. 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.

相关问题 Openpyxl:“工作表”对象没有属性“值” - Openpyxl: 'Worksheet' object has no attribute 'values' AttributeError:“工作表”对象在openpyxl(python)中没有属性“ get_highest_row” - AttributeError: 'Worksheet' object has no attribute 'get_highest_row' in openpyxl(python) xlsxwriter,openpyxl:“工作簿”对象没有“写”属性 - xlsxwriter, openpyxl: 'Workbook' object has no attribute 'write' 无法写入excel AttributeError:'Worksheet'对象没有属性'write' - Cannot write to an excel AttributeError: 'Worksheet' object has no attribute 'write' 得到错误; attributeerror:'Worksheet'对象没有属性'delete_rows'openpyxl - getting the error; attributeerror: 'Worksheet' object has no attribute 'delete_rows' openpyxl Openpyxl,max_highest_column给出错误。 工作表对象没有属性“ max_highest_column”? - Openpyxl, max_highest_column gives an error. Worksheet object has no attribute 'max_highest_column'? Python openpyxl模块说:AttributeError:'tuple'对象没有属性'upper' - Python openpyxl module says: AttributeError: 'tuple' object has no attribute 'upper' “工作表”对象没有属性“单元格” - 'Worksheet' object has no attribute 'cell' “工作表” object 没有属性“电子表格” - 'Worksheet' object has no attribute 'spreadsheets' Openpyxl - AttributeError: 'NoneType' 对象没有属性 'lower' - Openpyxl - AttributeError: 'NoneType' object has no attribute 'lower'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM