0

I have two excel files. One file is a table of data and the other file is a coversheet containing some dates/descriptive information.

Is there a way, preferably using openpyxl, where I can combine these two files (data and coversheet) into a new excel file (final) with one sheet containing the coversheet and a second sheet containing the data. I need to retain the formatting of the coversheet.

I can copy one file to another row by row

import openpyxl as op 
from openpyxl import Workbook

dest_wb = Workbook()

dest_wb.create_sheet("Cover Sheet")
dest_ws = dest_wb["Cover Sheet"]

source_wb = op.load_workbook(r"C:\Users\coversheet.xlsx")
source_sheet = source_wb.active

for row in source_sheet.rows:
    for cell in row:
        dest_ws[cell.coordinate] = cell.value

dest_wb.save(r"C:\Users\test.xlsx")

dest_wb.close()
source_wb.close()

But this doesn't retain any of the formatting from the original file

Gingerhaze
  • 664
  • 2
  • 5
  • 13
  • You're explicitly copying only values when you call `dest_ws[cell.coordinate] = cell.value` – picobit Nov 18 '22 at 20:47
  • Looks like you have to do it the hard way https://stackoverflow.com/questions/8440284/setting-styles-in-openpyxl#8441753 – picobit Nov 18 '22 at 21:15
  • Openpyxl basically works with cells. Assuming you are able to; It is much, much easier to do this with Xlwings since you are doing the same the move/copy sheets manually in Excel. – moken Nov 19 '22 at 01:01

0 Answers0