简体   繁体   English

Python Openpyxl在单元格中编写

[英]Python Openpyxl Writing in a cell

I cannot seem to write any value in an excel sheet. 我似乎无法在Excel工作表中写入任何值。 I open two files at the same time. 我同时打开两个文件。 I want to copy a value from file 1 to file 2. it gives the error 我想将值从文件1复制到文件2。它给出了错误

File

"C:\Python34\lib\site-packages\openpyxl\writer\dump_worksheet.py", line 214, in removed_method
    raise NotImplementedError 

Only the line with the writing part gives an error. 只有带有书写部分的行才会出现错误。 The function code is as follows 功能代码如下

def data_input(size):

    from openpyxl import load_workbook

    wb1 = load_workbook('150318 load matching_Storage_v4.xlsm',data_only=True)
    wb1s1 = wb1.get_sheet_by_name('Home load options')

    from openpyxl import Workbook
    wb2 = Workbook('Data',write_only=True)
    wb2s1 = wb2.create_sheet(0)
    wb2s1.title = "Consumption"

    wb2s1.cell(row = 1, column = 1).value = 4 - this line gives the error

    #what i have to write but block yet to test if i can write at all
    '''i = 0
    r = 0
    while i < 8760:
        d = wb2s1.cell(row = r, column = 1)
        d.value = i
        i = i + 0.25
        r += 1'''


    for i in range(4,35040):
        cell_value1 = wb1s1.cell(row = i, column = (12+size)).value
        print(cell_value1)
     #   cell_value1 = wb2s1.cell(row = i-3, column = 1) 
    wb2.save('Data.xlsx')

I tried all the different ways in the documentation but nothing works so far. 我尝试了文档中的所有不同方式,但到目前为止没有任何效果。

please help. 请帮忙。

Thank you 谢谢

You are creating a write-only workbook. 您正在创建一个只写工作簿。 At the name suggests, this is designed for streaming data to a workbook so some operations, such as looking up cells do not work. 顾名思义,这是为将数据流传输到工作簿而设计的,因此某些操作(例如查找单元格)不起作用。 To add data you should use the append() method. 要添加数据,您应该使用append()方法。 If you do need to add formatting or comments to individual cells you can include a WriteOnlyCell in the iterable that you pass into append() . 如果确实需要为单个单元格添加格式或注释,则可以在传递给append()的可迭代对象中包括WriteOnlyCell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM