There are several ways to copy an excel (.xlsx) sheet to another existing excel file. As i have to deal with sheets containing formatted form objects (colored text boxes, lines, ...), the only working solution was to use the win32.com.client
method, as mentioned here. Using that method, i can insert a sheet from one existing excel file in another existing excel file.
But doing so has a side effect i'd like to avoid:
Before adding the sheet, my destination file sheet contains content that fits on exactly one page when printing, or saving in PDF format.
After adding the sheet, my destination file sheet will result in 72 pages when printing or being saved in PDF format. First page contains the data, other 71 pages are plain white.
I'm pretty sure that's not a problem of the code, it looks like this:
from win32com.client import Dispatch
xl = Dispatch("Excel.Application")
wb1 = xl.Workbooks.Open(Filename=template)
wb2 = xl.Workbooks.Open(Filename=destination)
ws1 = wb1.Worksheets(1)
ws1.Copy(Before=wb2.Worksheets(1))
wb2.Close(SaveChanges=True)
xl.Quit()
This behaviour can of course be changed afterwards by defining the printing area manually in excel. It can also be changed by setting the print setup to only proccess page 1. But this is not the desired solution for this problem, as other people working with this files tend not to look at the printing preview and well, then you have this pile of slightly warm blank papers on the printer.
I tried to set the printing area after the sheet-copy-thing, using openpyxl
. This doesn't work, as saving the result deletes all form objects from my sheets, because openpyxl can't handle them. (Setting the printing area itself worked fine, but that could ony be done with files just containing data.)
So, i'm looking for:
A solution to add an entire existing .xlsx worksheet containing form objects to another existing excel file, without messing around with the printed outcome, or
A method to change the printing area of existing .xlsx sheets, without losing form objects or images.
I didn't find anything helpful - but maybe option 2 can be done using win32.com
, too?
Any idea or help is welcome, thank you!