anandamu16
anandamu16

Reputation: 123

python script to paste rows from 1 excel sheet to another. Paste should be "paste by value"

I am writing a python script to copy rows and columns from source to destination excel sheet. Source excel sheet has rows that have formulas. I just want to copy and paste values instead of the formula. Is there some way to do that in python? I have written the below code (it copies and paste the formulas instead of the actual value)

file_src="C:\\Users\\final_excel1.xlsx"
wb1 = openpyxl.load_workbook(file_src)
ws1 = wb1.worksheets[2] # index=n-1

file_dest = "C:\\Users\\final_excel2.xlsx"
wb2 = openpyxl.load_workbook(file_dest) 
ws2 = wb2.active

r_ws1=ws1.max_row
c_ws1=ws1.max_column

## copying values from source to dest.
for i in range  (1, req_r_ws1+1):
    for j in range (1, req_c_ws1+1):
        cell=ws1.cell(row=i, column=j)
        ws2.cell(row=i, column=j).value=cell.value

wb2.save(file_dest)

Upvotes: 0

Views: 845

Answers (1)

Zr.Ms. Bruinvis
Zr.Ms. Bruinvis

Reputation: 64

add data_only=True to wb1

wb1 = openpyxl.load_workbook(file_src, data_only=True)

also see, Read Excel cell value and not the formula computing it -openpyxl

Upvotes: 1

Related Questions