简体   繁体   English

如何在同一工作簿中使用python将数据从一个工作表传输到另一个工作表?

[英]How to transfer data from one worksheet into another using python in the same workbook?

I want to transfer my data which is in 'Sheet1' into worksheet 'Sheet2'in the same workbook, using python.I have written the script below, but facing an 'IndexError: list index out of range' . 我想使用python将'Sheet1'中的数据传输到同一工作簿中的工作表'Sheet2'中。我已经在下面编写了脚本,但是面临'IndexError:list index out of range' I know this is not the best way to go about it. 我知道这不是解决问题的最佳方法。 I will appreciate if anyone can guide me with a more efficient way to go about it. 如果有人可以用更有效的方法指导我,我将不胜感激。 I am sharing the Snapshot of excel file below 我正在共享下面的excel文件快照

Can I directly enter 'Sheet1' cell value ---> 'Sheet2' cell value rather than doing 'Sheet1' cell value ---> List---->'Sheet2' cell value ? 我可以直接输入“ Sheet1”单元格值--->“ Sheet2”单元格值,而不是执行“ Sheet1”单元格值---> List ---->“ Sheet2”单元格值吗?

在此处输入图片说明

 import openpyxl  
 wb = openpyxl.load_workbook('C:\Users\laban\Desktop\Par-emails1.xlsx') 
 type(wb)
 wb.get_sheet_names() 
 sheet = wb.get_sheet_by_name('Sheet1') 
 type(sheet)  
 anotherSheet = wb.active
 sheet1 = wb.get_sheet_by_name('Sheet2')
 type(sheet1)
 par=[sheet.cell(row= col, column=1).value for col in range(1, 2450)]
 email_no=[sheet.cell(row= col, column=2).value for col in range(1, 2450)]
 Domain=[sheet.cell(row= col, column=3).value for col in range(1, 2450)]
 email=[sheet.cell(row= col, column=4).value for col in range(1, 2450)]

 for x in range(0,2450):
      if email_no[x]<9:
          sheet1.cell(row= x+1, column=1).value=par[x]
          sheet1.cell(row= x+1, column=2).value=email_no[x]
          sheet1.cell(row= x+1, column=3).value=Domain[x]
          sheet1.cell(row= x+1, column=4).value=email[x]


 wb.save('C:\Users\laban\Desktop\Par-emails1.xlsx')

You can use: 您可以使用:

wb = openpyxl.load_workbook('C:/Users/laban/Desktop/Par-emails1.xlsx')
sheet1 = wb.get_sheet_by_name('Sheet1')
sheet2 = wb.get_sheet_by_name('Sheet2')

for i,row in enumerate(sheet1.iter_rows()):
  for j,col in enumerate(row):
    sheet2.cell(row=i+1,column=j+1).value = col.value

Apparently in 2.4 you can do this with one command: Copy whole worksheet with openpyxl 显然,在2.4中,您可以使用以下命令执行此操作: 使用openpyxl复制整个工作表

Obviously this is very simplified version from what I am currently using. 显然,这是我当前使用的非常简化的版本。 Here is a code snippet to copy data from sheet1 to sheet2 without any formatting. 这是一个代码片段,无需任何格式即可将数据从sheet1复制到sheet2。 You dont need to specify max rows , max columns as you can get it using sheet.max_row and sheet.max_columns methods. 您无需指定max rows和max column,就可以使用sheet.max_rowsheet.max_columns方法获得它。

from openpyxl.cell import Cell    

max_row = sheet1.max_row       #Get max row of first sheet
max_col = sheet1.max_column    #Get max column of first sheet

for row_num in range(1,max_row + 1):    #Iterate through rows
    for col_num in range(1, max_col + 1):   #Iterate through columns
        _cell1 = sheet1.cell(row=row_num, column=col_num)    
        _cell2 = sheet2.cell(row=row_num, column=col_num)    
        _cell2.value = _cell1.value

Added extra variables for understanding. 添加了额外的变量以供理解。 You can compact at your end. 您可以在最后压缩。

bernie probably has the best answer here (Copy whole worksheet) but your index error might be coming from: bernie可能是这里的最佳答案(复制整个工作表),但您的索引错误可能来自:

par=[sheet.cell(row= col, column=1).value for col in range(1, 2450)]

being 1 cell shorter than: 比1个单元格短:

for x in range(0,2450):
def excel_op():

   filename='D://Python//excelread.xlsx'
        sheet_name='Sheet1'
        book = xlrd.open_workbook(str(filename))

        sheet = book.sheet_by_name(sheet_name)
        workbook = xlsxwriter.Workbook('excelwrite.xlsx')
        worksheet = workbook.add_worksheet()

        row_count = int(sheet.nrows)
        col_count = int(sheet.ncols)
        for row in range(0, int(row_count)):
            for col in range(0, int(col_count)):
                worksheet.write(row,col,str(sheet.cell(row, col).value))
        workbook.close()
        del book
 shutil.copy("Source.xlsx", "target.xlsx")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 openpyxl 将工作表从一个工作簿复制到另一个工作簿? - How to copy worksheet from one workbook to another one using openpyxl? 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 将数据从一个工作表复制到另一个工作簿中的指定工作表 - Copying data from one worksheet to specified worksheet in another workbook 想要使用 openpyxl 将工作表从一个工作簿添加到另一个工作簿 - want to add worksheet from one workbook in to another workbook using openpyxl 如何使用tkinter在python中将数据从一个类传输到另一个类? - How to transfer data from one class to another in python using tkinter? 使用 openpyxl 将工作表(数据+样式)从工作簿复制到 Python 中的另一个 - Copy a worksheet (data+style) from a workbook to another in Python using openpyxl 将工作表从一个工作簿复制到另一个工作簿 - Copying a worksheet from one workbook to another 如何使用 python xlrd/xlwt 从一个 excel 工作簿和 output 将数据提取到另一个工作簿? - how to extract data from one excel workbook and output to another using python xlrd/xlwt? 如何将数据从一个mysql数据库传输到另一个数据库,以及如何使用python映射具有不同列名的数据 - how to transfer data from one mysql database to another and mapping the data with different column names using python python - 如何使用插入命令将数据从一个SQL服务器传输到另一个SQL服务器? - How to transfer the data from one SQL server to another using insert into command using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM