jeudi 9 septembre 2021

determine number of pages in an Excel sheet

In python am trying to figure out the number of pages of an excel sheet before converting it to a PDF. i.e. if the excel sheet takes up 1 page, only print 1 page. If the document is 3 pages, print 3 pages. Does anybody know how I would go about counting the pages of the Excel document? Particularly if the row height is changed so that rows 1-29 are on 1 page but if another XLS file has rows 1-37 on the first page. Below is my code and what each line does. Writing the proper conditions is where the help is needed.

from win32com import client # Imports Module

excel = client.Dispatch("Excel.Application") # Opens Microsoft Excel
sheets = excel.Workbooks.Open(file path.XLS) # Reads Excel File
work_sheets = sheets.Worksheets[0] # Reads sheet 1 of the workbook
work_sheets.PageSetup.FitToPagesWide = 1 # makes it 1 page wide

if worksheet is num pages pages:
    work_sheets.PageSetup.FitToPagesTall = num pages # need to figure out how to measure it
    
work_sheets.ExportAsFixedFormat(0, file_path.pdf) # Convert into PDF File, (0 = pdf, 1 = xps), path and name

Aucun commentaire:

Enregistrer un commentaire