在openpyxl load_workbook,use_iterators中设置值 [英] Setting values in openpyxl load_workbook, use_iterators

查看:358
本文介绍了在openpyxl load_workbook,use_iterators中设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取一个xlsx文件,将所有小于0.0001的值更改为0.01.我可以读取并打印这些值,但不能更改它们吗?

I want to read a xlsx file, change all the values less than say 0.0001 to 0.01. I can read the values and print them, but I can't change them ?

import pylab

from openpyxl import load_workbook
wb = load_workbook(filename = 'TF-Automation.xlsx', use_iterators=True)
ws = wb.get_sheet_by_name(name = 'Huvudmatris')

for row in ws.iter_rows():
    for cell in row:
        if cell.internal_value < 0.00001:
              cell.set_value = 0.000001      
        print cell.internal_value

从文档中

推荐答案

: http://pythonhosted.org/openpyxl/api.html

 openpyxl.reader.excel.load_workbook(filename,use_iterators=False)[source] :

Open the given filename and return the workbook
Parameters:   

    filename (string) – the path to open
    use_iterators (bool) – use lazy load for cells

Return type:  
    openpyxl.workbook.Workbook

使用延迟加载时,所有工作表将 openpyxl.reader.iter_worksheet.IterableWorksheet返回的 工作簿为只读.

When using lazy load, all worksheets will be openpyxl.reader.iter_worksheet.IterableWorksheet and the returned workbook will be read-only.

请勿使用use_iterators=True.另外,如果要使用新值更新xlsx,则需要调用.save(filename).

don't use use_iterators=True . Also, if you need to call .save(filename) if you want to update the xlsx with your new values.

这篇关于在openpyxl load_workbook,use_iterators中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆