将数据从一张Excel表复制到另一张

2024-05-16 20:20:13 发布

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

我对编码还是相当陌生的,所以我确信有更简单或更漂亮的方法来编写下面的脚本。脚本将运行,新工作表将在工作簿中创建,但是,数据不会从“sheet1”复制到第二个工作表

我尝试过谷歌搜索和阅读其他关于堆栈溢出的线程,但似乎没有人能回答这个问题

import os, csv, glob, shutil, pandas as pd, numpy as np, openpyxl as opyx

path_to_combined_file = c:\\somefilepath here\\

filepath = path_to_combined_file + 'NSW.xlsx'

unaided_brand_awareness = pd.read_excel(filepath)

from openpyxl import load_workbook
wb = load_workbook(filepath)
wb.create_sheet('unaided_brand_awareness')
wb.create_sheet('aided_brand_awareness')
wb.create_sheet('favourite_stations')

worksheet1 = wb['Sheet1']
worksheet2 = wb['unaided_brand_awareness']

for i in range (1,2000):
    for j in range(1, worksheet1.max_column + 1):
        worksheet2.cell(row = i, column = j).value = worksheet1.cell(row = i, column = j).value

wb.save(filepath)

代码应创建以下表格“独立品牌意识”、“独立品牌意识”和“最喜爱的电台”,然后将数据从标题为“Sheet1”的表格复制到标题为“独立品牌意识”的表格中

理想情况下,最好将“Sheet1”中的数据复制到工作簿中的所有工作表中。你知道吗

另外,我可能应该注意到“Sheet1”中包含的单元格数会因情况而异。你知道吗


Tags: 数据脚本ascreatecolumn表格sheetwb