python - 合并两个带有多个工作表的excel文件而不会丢失格式
问题描述
我想分别使用python将多个 excel 文件与多个工作表合并。我不想丢失工作表中的任何格式。它应该复制所有工作表并只创建一个 Excel 文件。
我只能合并第一张纸,而且所有格式都丢失了。
这是我的代码:
import os
import os.path
import xlrd
import xlsxwriter
file_name = input("merge")
merged_file_name = file_name + ".xls"
dest_book = xlsxwriter.Workbook('m.xls')
dest_sheet_1 = dest_book.add_worksheet()
dest_row = 1
temp = 0
path = input("C:\\test")
out = os.path.isdir("")
print(out)
print("File path: " + path)
for root,dirs, files in os.walk("C:\\test"):
for xlsfile in files:
print ("File in mentioned folder is: " + xlsfile)
temp_book = xlrd.open_workbook(os.path.join(root,xlsfile))
temp_sheet = temp_book.sheet_by_index(0)
if temp == 0:
for col_index in range(temp_sheet.ncols):
str = temp_sheet.cell_value(0, col_index)
dest_sheet_1.write(0, col_index, str)
temp = temp + 1
for row_index in range(1, temp_sheet.nrows):
for col_index in range(temp_sheet.ncols):
str = temp_sheet.cell_value(row_index, col_index)
dest_sheet_1.write(dest_row, col_index, str)
dest_row = dest_row + 1
dest_book.close()
book = xlrd.open_workbook("m.xls")
sheet = book.sheet_by_index(0)
print ("number of rows in destination file are: "), sheet.nrows
print ("number of columns in destination file are: "), sheet.ncols
解决方案
由于您需要 Excel 的特定需求(如格式化),因此请考虑使用 COM 接口直接连接到 Excel 对象库。当然,这假设您在机器上安装了 Excel。对于 Windows,Python 可以使用该win32com
库运行 COM,这不仅可以连接 Excel,还可以连接到大多数 Windows 应用程序和对象,包括记事本、Paint 甚至 ADODB。
本质上,这使用Workbooks.Add、Sheets.Add、Range.Copy和其他方法镜像 VBA(它与 Excel 对象库具有类似的接口) 。所有其他 API,例如xlrd
并且xlwriter
不直接使用 Excel 方法,因此您甚至会丢失图形而不是数据的格式。
import os
import win32com.client as win32
path = input("C:\\test")
file_name = input("merge")
merged_file_name = file_name + ".xlsx"
try:
# INITIALIZE EXCEL COM APP
xlapp = win32.gencache.EnsureDispatch('Excel.Application')
# ASSIGN CONSTANTS
xlPasteValues = -4163; lPasteFormats = -4122; xlWorkbookDefault = 51
# CREATE NEW WOKRBOOK (PROMPTS IF EXISTS)
new_wb = xlapp.Workbooks.Add()
new_wb.SaveAs(Filename='MasterMerge.xlsx', FileFormat=xlWorkbookDefault)
# LOOP THROUGH WORKBOOKS
xl_files = [f for f in os.listdir(path) if f.endswith('.xls') or f.endswith('.xlsx')]
for wb in xl_files:
xlwb = xlapp.Workbooks.Open(os.path.join(path, wb))
# LOOP THROUGH EVERY WORKSHEET, COPYING TO NEW WORKSHEET
for xlsh in xlwb.Worksheets:
new_sh = new_wb.Worksheets.Add()
new_sh.Name = xlsh.Name
new_wb.Save()
new_sh.Move(After=new_wb.Worksheets(new_wb.Worksheets.Count))
xlsh.Cells.Copy(new_sh.Cells)
new_sh = None
xlwb.Close(False)
xlwb = None
# REMOVNIG DEFAULT SHEET AND LAUNCHING TO SCREEN
new_wb.Worksheets('Sheet1').Delete()
new_wb.Save()
xlapp.Visible = True
except Exception as e:
print(e)
finally:
# RELEASE RESOURCES
xlsh = None; new_sh = None;
xlwb = None; new_wb = None; xlapp = None
推荐阅读
- database - OrientDB 中缺少记录 OUser
- apache - 即使在配置目录、文件 chmod 和 selinux 设置后也禁止 Apache 403
- python - try: VAR = ... except: VAR = ... finally: VAR - 局部变量 'VAR' 可能在赋值之前被引用
- c - 为什么暂定定义不会由于多个定义而导致错误?
- python - powerBI 中的 Pandas (Python)
- reactjs - 如何阻止我的材质 UI 卡离开屏幕创建水平滚动情况?
- javascript - Chrome 扩展程序未发送请求
- angular - 角度组件问题
- javascript - math.trunc() 返回带小数的值
- node.js - Express & mangoDB 仅在所有数据有效时才保存