如何用简单的方法实现将A excel中指定列的数据追加到B excel指定列的数据后面
可以用openpyxl 这个库,读取和写入excel
是直接接着那一列的话复制粘贴
在 Python 中,你可以使用第三方库 pandas 来处理 Excel 文件。下面是一个简单的示例,展示了如何将 A Excel 文件中指定列的数据追加到 B Excel 文件的指定列后面:
import pandas as pd
# 读取 A Excel 文件的指定列数据
df_a = pd.read_excel('A.xlsx', usecols=['指定列名'])
# 读取 B Excel 文件
df_b = pd.read_excel('B.xlsx')
# 将 A Excel 的数据追加到 B Excel 的指定列后面
df_b['指定列名'] = pd.concat([df_b['指定列名'], df_a['指定列名']], ignore_index=True)
# 保存修改后的 B Excel 文件
df_b.to_excel('B.xlsx', index=False)
上述代码中的 'A.xlsx' 和 'B.xlsx' 分别是你要处理的 A Excel 文件和 B Excel 文件的文件名。'指定列名' 需要替换为你要追加的列的列名。
可以用Spire.XLS for Python库。pip install安装命令:
pip install Spire.XLS-for-Python
pip install plum-dispatch==1.7.4
获取指定列数据:
from spire.xls import *
from spire.common import *
#Create a workbook
workbook = Workbook()
#Load an Excel file
workbook.LoadFromFile("Sample.xlsx")
#Get the first worksheet of the file
worksheet = workbook.Worksheets[0]
#Get the cells collection of the first column
cellRangeCollection = worksheet.Columns[0].Cells
str =""
#Iterate through the cells in the collection
for cellRange in cellRangeCollection:
#Get the values of the cells
result = "Cell: " + cellRange.RangeAddress + " Value: " + cellRange.Value
str = str + result +"\n"
print(str)
workbook.Dispose()
写入另一个文件的另一列也很简单了,直接加载另一个文档,通过列索引获取另一列和它的单元格集合(worksheet.Columns[int index].Cells),循环遍历每个单元格,然后通过单元格的Value属性赋值,跟上面代码类似
不知道你这个问题是否已经解决, 如果还没有解决的话:excel中导入的时间在读取的时候是一串数字,类似于456374.234134
读取方法是:
# 变为元祖,第二个参数有两种取值,0或者1,0是以1900-01-01为基准的日期,而1是1904-01-01为基准的日期。
wf_time = str(xlrd.xldate.xldate_as_datetime(Excel_time, 0))
但这样设置的话,如果传来的书正常的时间字符串,就会抛出异常
所以建议两种时间格式都要考虑进去.