怎样实现将不同文件夹下相同名称的excel文件合并到一个excel文件中
https://blog.csdn.net/weixin_47036600/article/details/129400596
第一步准备先试试把一个表格的内容全部导入新的表格中,相当于全部复制粘贴。
lines = open('表格名称.csv','r',encoding='gbk').readlines()
n = ''
for line in lines:
n = n+line
with open('new form 1.csv','w',encoding='gbk') as new_f:
new_f.write(n)
new_f.close()
第一步成功
可以用Spire.XLS for Python,下面代码里是文件名,你写成完整路径就行
from spire.xls import *
from spire.common import *
#Create a list of file paths for the Excel files to be merged
files = []
files.append("File-1.xlsx" )
files.append("File-2.xlsx")
files.append("File-3.xlsx")
#Create a new workbook
newbook = Workbook()
newbook.Version = ExcelVersion.Version2013
#Clear all default worksheets
newbook.Worksheets.Clear()
#Create a temporary workbook
tempbook = Workbook()
#Iterate through each file in the list of file paths
for file in files:
#Load the file into the temporary workbook
tempbook.LoadFromFile(file)
#Iterate through each worksheet in the temporary workbook
for sheet in tempbook.Worksheets:
#Copy the worksheet from the temporary workbook to the new workbook
newbook.Worksheets.AddCopy(sheet, WorksheetCopyType.CopyAll)
outputFile = "MergeExcelFiles.xlsx"
#Save the merged file to a specific location
newbook.SaveToFile(outputFile, ExcelVersion.Version2013)
newbook.Dispose()
tempbook.Dispose()
具体可以参考这篇文章(要梯子)