簡體   English   中英

如何在同一工作簿中使用python將數據從一個工作表傳輸到另一個工作表?

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

我想使用python將'Sheet1'中的數據傳輸到同一工作簿中的工作表'Sheet2'中。我已經在下面編寫了腳本,但是面臨'IndexError:list index out of range' 我知道這不是解決問題的最佳方法。 如果有人可以用更有效的方法指導我,我將不勝感激。 我正在共享下面的excel文件快照

我可以直接輸入“ 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')

您可以使用:

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

顯然,在2.4中,您可以使用以下命令執行此操作: 使用openpyxl復制整個工作表

顯然,這是我當前使用的非常簡化的版本。 這是一個代碼片段,無需任何格式即可將數據從sheet1復制到sheet2。 您無需指定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

添加了額外的變量以供理解。 您可以在最后壓縮。

bernie可能是這里的最佳答案(復制整個工作表),但您的索引錯誤可能來自:

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

比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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM