2022年10月6日 星期四

Copy and Paste the source Excel file to destination Excel file using Python openpyxl module

# Copy and Paste the source Excel file to destination Excel file using Python openpyxl module

# importing openpyxl module

import openpyxl as xl


# opening the source excel file

filename = "C:/Users/gary/file1.xlsx"

wb1 = xl.load_workbook(filename)

ws1 = wb1.worksheets[0]


# opening the destination excel file

filename1 = "C:/Users/gary/file2.xlsx"

wb2 = xl.load_workbook(filename1)

ws2 = wb2.active


# calculate total number of rows and

# columns in source excel file

mr = ws1.max_row

mc = ws1.max_column


print(mr)

print(mc)


# copying the cell values from source

# excel file to destination excel file

for i in range (1, mr + 1):

    for j in range (1, mc + 1):

        # reading cell value from source excel file

        c = ws1.cell(row = i , column = j)


        # writing the read value to destination excel file

        ws2.cell(row = i , column = j).value = c.value


# saving the destination excel file

wb2.save(str(filename1))

沒有留言:

張貼留言

Python program to display calendar

# Python program to display calendar of given month of the year # importing calendar module for calendar operations import calendar # set t...