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