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
You can make multiple sheets active using Openpyxl but cannot print the sheets using this module so not sure if it's the best option for your purposes.
However if you do want to do this the following code will help.
For setting the sheet to active you may need/want to include a criteria (based on the sheet name or other) to determine if the sheet should be set active.
After saving, when the workbook is opened in Excel those Sheets will all be selected and will be the ones printed if 'Print Active Sheets' is used.
Remember the default active sheet in the workbook may not be one you want selected so that sheet may need to be deselected.
...
### De-Active-ate the current active Sheet
wb[wb.active.title].views.sheetView[0].tabSelected = False
### Set selected Sheets as Active
for sheet in wb.worksheets:
if sheet.title <matches required criteria>:
sheet.views.sheetView[0].tabSelected = True
If you want to actually print as well then a module like Xlwings or Win32com would need to be used.
You can do something similar and select sheets (make them active);
Select Sheets by name that match a criteria into a list then 'select' the Sheets together using the List. The saved workbook would then have these Sheets active when the opened.
With visible=True
you will see the opened workbooks and the Sheets being selected when this part of the code runs.
import xlwings as xw
with xw.App(visible=True) as app:
wb = xw.Book('foo.xlsx')
sheet_list = []
for s_name in wb.sheet_names:
if s_name <match criteria>:
sheet_list.append(s_name)
wb.api.Sheets(sheet_list).Select()
wb.save('foo_out.xlsx')
However you don't need to save this state for printing, you can print the selected Sheets in that code execution.
The following code will print just those Sheets that are selcted into the sheet_list List. If that is all that you required there is no need to make the selection and save the workbook.
import xlwings as xw
with xw.App(visible=True) as app:
wb = xw.Book('foo.xlsx')
sheet_list = []
for s_name in wb.sheet_names:
if s_name <match criteria>:
sheet_list.append(s_name)
wb.api.Sheets(sheet_list).PrintOut(Copies=1)
If you want to print the whole workbook then just print without any selections;
wb.api.PrintOut(Copies=1)
all Sheets will be printed.
Win32com would use similar commands.