将数据从一个 excel 工作表传输到另一个

Transfer data from one excel worksheet to another

我有一个 excel 文件,其中所有数据都按行列出,我需要获取这些数据并将其列在新创建的工作簿中各个工作表的 A 列中。我不太明白语法是否正确。请协助

import openpyxl
import os
import time

wb = openpyxl.load_workbook('IP-Results.xlsx') #load input file
sheet = wb.get_sheet_by_name('IP-Results-32708') #get sheet from input file

wbOutput = openpyxl.Workbook() #open a new workbook
wbOutput.remove_sheet(wbOutput.get_sheet_by_name('Sheet')) #remove initial worksheet named 'sheet'

for cell in sheet['A']: #iterate through firewall names in column A and make those the title of the sheets in new workbook
    value = cell.value
    wbOutput.create_sheet(title=cell.value)



for rowOfCellObjects in sheet.iter_rows():
    for data in rowOfCellObjects:
        if data.value != None:
            for worksheet in wbOutput.get_sheet_names():
                worksheet.cell(row=1, column=1).value = data


wbOutput.save("Decom-" + time.strftime("%d-%m-%Y")+ ".xlsx")

此代码出现以下错误:

c:\Python35\Scripts>python decom.py 追溯(最近一次通话): 文件 "decom.py",第 21 行,位于 ws=worksheet.active AttributeError: 'str' 对象没有属性 'active'

wbOutput.get_sheet_names() returns list 个字符串。

要引用工作表对象,请使用 wbOutput.worksheets[0]

替换:

for worksheet in wbOutput.get_sheet_names():

;与

for worksheet in wbOutput.worksheets: