简体   繁体   English

使用 python 将多个列从 excel 表的 file1 复制到 file2 的其他列/行 其他 excel 表,使用 pandas 或其他任何东西

[英]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.

相关问题 有没有办法将特定列从 Excel 工作表(例如 sheet_1)复制到 sheet_2 中的另一列? 使用 Python - Is there any way to copy a particular column from excel sheet (say sheet_1) to the other column which is in sheet_2? Using Python 使用 sed 或任何其他命令将部分行从 file1 复制到 file2 中的特定位置 - copy a part of a line from file1 to specific place in file2 using sed or any other command 使用 python 将列从一个 excel 文件复制到另一个 excel 文件表 - Copy columns from one excel file to another excel file sheet using python 将file1的每一行复制到file2的其他每行中(Python) - Copy each line of file1 into every other line of file2 (Python) 使用python(或其他)从大csv文件创建Excel数据表 - Creating an Excel data sheet from big csv file using python (or other) 在工作表中添加一列,然后使用python pandas或任何其他更好的库在整个列中应用此excel公式 - Add a column to sheet and apply this excel formula in whole column using python pandas or any other better library 使用python反转Excel工作表中的2列,其中一列具有单个变量,另一列具有列表 - Reversing 2 columns in an excel sheet with one column having a single variable and the other column having a list using python Python / Pandas从Excel工作表复制和粘贴 - Python/Pandas copy and paste from excel sheet 从 pandas 到一张 excel 工作表的多个数据帧,而不用 python 删除其他工作表 - multiple dataframes from pandas to one excel sheet without deleting other sheets with python 我可以从Excel文件修改特定工作表并回写到同一工作表而不使用Pandas |修改其他工作表 openpyxl - Can I modify specific sheet from Excel file and write back to the same without modifying other sheets using Pandas | openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM