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

2024-09-28 19:25:50 发布

您现在位置:Python中文网/ 问答频道 /正文

Input FileOutput File

我有一个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装饰材料 回溯(最近一次呼叫): “文件”装饰材料“,第21行,英寸 远景=工作表.active AttributeError:“str”对象没有属性“active”


Tags: 文件数据inimportfordatagettime
1条回答
网友
1楼 · 发布于 2024-09-28 19:25:50

wbOutput.get_sheet_names()返回一个list字符串。在

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

替换:

for worksheet in wbOutput.get_sheet_names():

;与

for worksheet in wbOutput.worksheets:

相关问题 更多 >