简体   繁体   English

如何使用python的xlsx从一个excel文件中复制多列并根据用户需要将它们粘贴到另一个excel文件中?

[英]How to copy multiple column from one excel file and paste them according to user's need in another excel file using python's xlsx?

I want to copy a column from an excel file and then paste it in another using python.我想从 excel 文件中复制一列,然后使用 python 将其粘贴到另一个文件中。

I have tried xlsx and read the column one by one and converted it to a list and then make a for loop over that list to write in new excel file我已经尝试过 xlsx 并一一读取该列并将其转换为一个列表,然后对该列表进行 for 循环以写入新的 excel 文件

Note: The get_index function is reading the column from file1 and converting to a list and write_t0_col function is printing that list to new excel file but it's printing the first list in all the columns注意:get_index function 正在从 file1 读取列并转换为列表,而 write_t0_col function 正在将该列表打印到新的 ZBF57C906FA7D2BB66D07372E4158D9 列中的第一个列中

def get_index(a, workbook):
    global c1
    List = []
    for i in range(sheet.nrows):
        c1 = sheet.cell_value(i, a)
        convert(c1, List)
    write_to_col(List, workbook)
    print(List)

def write_to_col(data, workbook):  # function to write the list into a column
    row = 0
    col = 0
    for item in data:
        for col in range(5):
            worksheet.write(row, col, item)
        row += 1
    #col+=1

    workbook.close()

output that i want is to print all the list into column one by one as they are called from user.我想要的 output 是将所有列表一一打印到列中,因为它们是从用户调用的。

If you only want to copy and paste excel columns with python, using pandas is enough:如果您只想复制和粘贴 excel 列和 python,使用pandas就足够了:

import pandas as pd
# read excel
xl = pd.ExcelFile("originExcel.xlsx")
df = xl.parse("your sheet name")

# get the column you want to copy
column = df["your column name"]

# paste it in the new excel file
with pd.ExcelWriter('newExcel.xlsx', mode='w') as writer:
    column.to_excel(writer, sheet_name= "new sheet name", index = False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将具有相同列名的列数据的多个工作表复制并粘贴到另一个 excel 文件 - How to copy and paste multiple sheet for column's data with the same column name to another excel file 将 excel 文件内容从一个 excel 复制并粘贴到另一个,并使用 python 保留超链接 - Copy and paste excel file contents from one excel to another and also preserve the hyperlinks using python 如何使用python将数据从excel复制并粘贴到另一个excel - How to copy and paste data from excel to another excel using python 如何将一个CSV文件中的多行和一列复制到另一个CSV Excel中? - How to copy multiple rows and one column from one CSV file to another CSV Excel? 如何将数据从多个工作表的一个Excel文件复制到多个工作表的另一个Excel文件中 - How do I copy the data from one Excel file of multiple sheets to another Excel file of multiple sheets 使用 Spreadsheett::ParseXLSX 从 .xlsx 文件复制单元格格式以使用 EXCEL:WRITER::XLSX 编写另一个单元格? - Copy the cell format from .xlsx file using Spreadsheett::ParseXLSX to write another cell using EXCEL:WRITER::XLSX? 使用 Python 有没有办法将数据从一个 excel 文件复制到另一个具有不同字段的 excel 文件 - Using Python Is there any way to copy data from one excel file to another excel file that has a different fields 如何使用 Python 将文本文件复制并粘贴到指定的 Excel TAB 中? - How to copy and paste text file into a specified Excel TAB using Python? 使用 python 将列从一个 excel 文件复制到另一个 excel 文件表 - Copy columns from one excel file to another excel file sheet using python 如何使用 Python 从 Words (.docx) 复制段落(带数字格式)和图像并粘贴到 Excel (.xlsx) - How to copy paragraphs (with number formatting) and images from Words (.docx) and paste into Excel (.xlsx) using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM