I would like to set several worksheets active in an Excel file, or change the default print settings to “Print entire workbook”.
This can be done manually in Excel. But I would like to write this to the files automatically.
The background is this: I have Excel files with multiple sheets. These should be printed with a right click, i.e. all sheets or the active ones.
Hier mein Ansatz:
from openpyxl import Workbook,load_workbook
from openpyxl.styles import Alignment
wb = load_workbook(filename)
sheets = wb.sheetnames
for sheet in sheets:
ws = wb[sheet]
alig = Alignment(horizontal='left', vertical='top',wrapText=True, shrinkToFit = True)
# Einstellungen zum drucken
ws.print_title_rows = '1:1' # erste Zeile
# ws.print_title_cols = 'A:A' # erste spalte
ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE # Querformat
ws.page_setup.paperSize = ws.PAPERSIZE_A4 # Papiergröße
ws.sheet_properties.pageSetUpPr.fitToPage = True
ws.page_setup.fitToHeight = False # alle Zeilen auf einer seite
ws.page_setup.fitToWidth = True # alle Spalten auf einer seite
# angaben in INCH
ws.page_margins.header = 0.393701 # Kopfzeile
ws.page_margins.footer = 0.0 # Fusszeile
ws.page_margins.left = 0.393701 # links
ws.page_margins.right = 0.393701 # rechts
ws.page_margins.bottom = 0.393701 # unten
ws.page_margins.top = 0.787402 # oben
# Kopf unf Fußzeile
ws.HeaderFooter.differentFirst = True
# ws.HeaderFooter.firstFooter.left.text = sheet
ws.HeaderFooter.firstHeader.right.text = sheet
wb.active = 0 ############## an dieser Stelle möchte ich alle/bestimmte Blätter auswählen bzw. aktiv setzten, oder, wahrscheinlich in den ws.sheet_properties.pageSetUpPr, die defaulteinstellung auf "Gesamte Arbeitsmappe drucken" umstellen.
wb.save(filename)
wb.close()
Does anyone have an idea how this can be implemented? Is there this print default in Openpyxl, or how can I activate multiple sheets and save them at the same time (it can be done individually).
Thanks