close

How to copy over an Excel sheet to another workbook in Python

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to copy over an Excel sheet to another workbook in Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to copy over an Excel sheet to another workbook in Python?

  1. How to copy over an Excel sheet to another workbook in Python?

    This might help if you're not opposed to using Pandas
    import pandas as pd #change xxx with the sheet name that includes the data data = pd.read_excel(sourcefile, sheet_name="xxx")

  2. copy over an Excel sheet to another workbook in Python

    This might help if you're not opposed to using Pandas
    import pandas as pd #change xxx with the sheet name that includes the data data = pd.read_excel(sourcefile, sheet_name="xxx")

Method 1

A Python-only solution using the openpyxl package. Only data values will be copied.

import openpyxl as xl

path1 = 'C:\\Users\\Xukrao\\Desktop\\workbook1.xlsx'
path2 = 'C:\\Users\\Xukrao\\Desktop\\workbook2.xlsx'

wb1 = xl.load_workbook(filename=path1)
ws1 = wb1.worksheets[0]

wb2 = xl.load_workbook(filename=path2)
ws2 = wb2.create_sheet(ws1.title)

for row in ws1:
    for cell in row:
        ws2[cell.coordinate].value = cell.value

wb2.save(path2)

Method 2

This might help if you’re not opposed to using Pandas

import pandas as pd

#change xxx with the sheet name that includes the data
data = pd.read_excel(sourcefile, sheet_name="xxx")

#save it to the 'new_tab' in destfile
data.to_excel(destfile, sheet_name='new_tab')

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read