[英]copy multiple columns from file1 of an excel sheet to other column/row of file2 other excel sheet using python , use pandas or anything else
I need to copy from file1 (I), from Ith column entire row after 9th cell and paste the data into D column from row 4 into file 2 using python.我需要从文件 1 (I)、第 9 个单元格后的第 I 列整行复制,并使用 python 将数据从第 4 行粘贴到 D 列到文件 2 中。 Also again I need to copy from file1 from b4 to b8 into file2 as J7 to j11.
同样,我需要将 file1 从 b4 复制到 b8 到 file2 作为 J7 到 j11。
i tried this code:我试过这个代码:
import openpyxl as xl
from openpyxl import load_workbook
import pandas as pd
# opening the source excel file
filename1 ="elementsheetsample.xlsx"
wb1 = xl.load_workbook(filename1)
ws1 = wb1['Sheet1']
# opening the destination excel file
filename2 ="filledelementsheetsample.xlsx"
wb2 = xl.load_workbook(filename2)
ws2 = wb2['INACTIVE Sheet']
i1=9 #I row in file1
i2=4 #D row in file2
# calculate total number of rows and
# columns in source excel file
mr = ws1.max_row
mc = ws1.max_column
# copying the cell values from source
if i1==2:
if i2==2:
for j1 in range (2, mc +1):
for j2 in range (2, mc + 1):
c = ws1.cell(row = i1, column = j1)
ws2.cell(row = i2, column = j2).value = c.value
updatedtx = "updatedtx2.xlsx"
# saving the destination excel file
wb2.save(str(updatedtx))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.