python 脚本将行从 1 excel sheet 粘贴到另一个。粘贴应该是 "paste by value"
python script to paste rows from 1 excel sheet to another. Paste should be "paste by value"
我正在编写一个 python 脚本来将行和列从源复制到目标 excel sheet。
Source excel sheet 包含包含公式的行。我只想复制并粘贴值而不是公式。在 python 中有什么方法可以做到这一点吗?
我写了下面的代码(它复制并粘贴了公式而不是实际值)
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)
将 data_only=True 添加到 wb1
wb1 = openpyxl.load_workbook(file_src, data_only=True)
另见,Read Excel cell value and not the formula computing it -openpyxl
我正在编写一个 python 脚本来将行和列从源复制到目标 excel sheet。 Source excel sheet 包含包含公式的行。我只想复制并粘贴值而不是公式。在 python 中有什么方法可以做到这一点吗? 我写了下面的代码(它复制并粘贴了公式而不是实际值)
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)
将 data_only=True 添加到 wb1
wb1 = openpyxl.load_workbook(file_src, data_only=True)
另见,Read Excel cell value and not the formula computing it -openpyxl