【问题标题】:writing to existing workbook using xlwt [closed]使用 xlwt 写入现有工作簿 [关闭]
【发布时间】:2011-02-13 03:04:13
【问题描述】:

我找不到使用 xlwt 写入现有文件的示例。我有一个需要写入的现有 xls 文件。当我使用 xlrd 读取文件时,我似乎无法弄清楚如何将返回的“Book”类型转换为 xlwt.Workbook。如果有人能给我举个例子,我将不胜感激。

【问题讨论】:

    标签: python xlwt xlrd


    【解决方案1】:

    这是我最近用来做这件事的一些示例代码。

    它打开一个工作簿,向下行,如果满足条件,它会在行中写入一些数据。最后保存修改后的文件。

    from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils
    from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd
    
    START_ROW = 297 # 0 based (subtract 1 from excel row number)
    col_age_november = 1
    col_summer1 = 2
    col_fall1 = 3
    
    rb = open_workbook(file_path,formatting_info=True)
    r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file
    wb = copy(rb) # a writable copy (I can't read values out of this, only write to it)
    w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy
    
    for row_index in range(START_ROW, r_sheet.nrows):
        age_nov = r_sheet.cell(row_index, col_age_november).value
        if age_nov == 3:
            #If 3, then Combo I 3-4 year old  for both summer1 and fall1
            w_sheet.write(row_index, col_summer1, 'Combo I 3-4 year old')
            w_sheet.write(row_index, col_fall1, 'Combo I 3-4 year old')
    
    wb.save(file_path + '.out' + os.path.splitext(file_path)[-1])
    

    【讨论】:

    • 这应该是公认的答案
    • 谢谢。这是一个非常有用的答案。
    • @chefsmart 是的,应该是。另外,格雷格回答的时候不是个混蛋。 +1
    • 这个程序不是默默地扔掉了它不能翻译的所有东西:VB 宏、大部分图形、高级格式和页面布局吗?
    • 是的,它看起来确实如此。我对其进行了测试,它抛弃了我的格式。
    【解决方案2】:

    您需要xlutils.copy。试试这样的:

    from xlutils.copy import copy
    w = copy('book1.xls')
    w.get_sheet(0).write(0,0,"foo")
    w.save('book2.xls')
    

    请记住,默认情况下您无法覆盖单元格,如 this question 中所述。

    【讨论】:

    • 我得到一个错误:AttributeError: 'str' object has no attribute 'formatting_info' 你知道为什么会这样吗?
    • 运行上面的sn-p的时候?发布您的代码。
    【解决方案3】:

    代码示例正是这样的:

    from xlutils.copy import copy
    from xlrd import *
    w = copy(open_workbook('book1.xls'))
    w.get_sheet(0).write(0,0,"foo")
    w.save('book2.xls')
    

    您需要创建 book1.xls 来进行测试,但您明白了。

    【讨论】:

      【解决方案4】:

      openpyxl

      # -*- coding: utf-8 -*-
      import openpyxl
      file = 'sample.xlsx'
      wb = openpyxl.load_workbook(filename=file)
      # Seleciono la Hoja
      ws = wb.get_sheet_by_name('Hoja1')
      # Valores a Insertar
      ws['A3'] = 42
      ws['A4'] = 142
      # Escribirmos en el Fichero
      wb.save(file)
      

      【讨论】:

      • 虽然这段代码可能会解决问题,但您应该始终解释它的作用以及它为什么有用。
      【解决方案5】:

      我遇到了同样的问题。我的客户向我订购了更新 XLS(不是 XLSX)Excel 文件的 Python 3.4 脚本。

      第一个包 xlrd 是由“pip install”安装的,在我的 Python 主页中没有问题。

      第二个 xlwt 需要说“pip install xlwt-future”才能兼容。

      第 3 个 xlutils 不支持 Python 3,但我对其进行了一些调整,现在它至少适用于虚拟脚本:

      #!C:\Python343\python
      from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils
      from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd
      from xlwt import easyxf # http://pypi.python.org/pypi/xlwt
      
      file_path = 'C:\Dev\Test_upd.xls'
      rb = open_workbook('C:\Dev\Test.xls',formatting_info=True)
      r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file
      wb = copy(rb) # a writable copy (I can't read values out of this, only write to it)
      w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy
      w_sheet.write(1, 1, 'Value')
      wb.save(file_path)
      

      我在此处附上文件:http://ifolder.su/43507580

      如果过期,请写信给 alexander.samoylov@gmail.com。

      P.S.:在虚拟示例中没有调用某些函数,因此它们可能也需要进行调整。谁想这样做,在谷歌帮助下一个一个地修复异常。这不是一个非常困难的任务,因为包代码很小......

      【讨论】:

        猜你喜欢
        • 2017-11-09
        • 1970-01-01
        • 1970-01-01
        • 2014-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-27
        • 2011-07-21
        相关资源
        最近更新 更多