【问题标题】:Inserting to certain columns in excel插入到excel中的某些列
【发布时间】:2020-07-03 12:56:25
【问题描述】:

使用python包openpyxl我正在尝试将列表项插入特定列C,例如ws['C'].append([Column])

这样做会产生错误AttributeError: 'tuple' object has no attribute 'append'

不确定如何在工作表的特定列中插入项目。

import sys 
from openpyxl import (load_workbook, Workbook)

try:
    def getExcel(spreadSheet):
        Columns=[]
        wb = load_workbook(spreadSheet)
        sheet = wb['sheet1']
        for cell in sheet['M']:
            if cell.value is not None: 
                Columns.append(cell.value)
            else: 
                continue
        print(len(Columns))
        wb = Workbook()
        dest_filename = 'new.xlsx'
        ws = wb.active
        for Column in Columns:
            ws['C'].append([Column])
        wb.save(dest_filename)
        print('Complete')

except ValueError:
    print ('failed...')
    sys.exit()

【问题讨论】:

  • 你能澄清你的问题吗?
  • 已更新,我认为这很清楚,在这种情况下,我试图在特定列中插入列表项 C 并抛出上面发布的错误。

标签: python excel python-3.x pip openpyxl


【解决方案1】:

因此,经过一番研究,发现可以在列中使用数字而不是字母,并且看到 cell() 如何期望列和行属性,我能够在我想要的列中插入项目。

递增i 允许将项目插入到同一列中的每个单元格中。

i = 1 
for Column in Columns:
            ws.cell(row = i, column = 3).value = column
             i = i + 1

【讨论】:

猜你喜欢
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多