0

I have a script using OpenPyXl to merge multiple reports into one, and to perform Excel formatting. This is working.

The final deliverable is a PDF. Unfortunately OpenPyXl doesn't have this functionality. I'm currently running my script, then using a third-party tool to manually print to PDF. I'm having trouble finding a Python-based solution to add Save/Print/Export-to-PDF into my script.

Pandas to PDF won't work, because all of the Excel formatting will be lost. The oldest solution and even a recent suggestion is to use PyWin32.

A highly scored answer from 2015 is offered by user @milowh74 in a question: Print chosen worksheets in excel files to pdf in python. This answer has many of the features I need. What's more, the user also links to a script with even more features. My final solution requires these standard features from Excel's Print modal:

  • Print Selection (aka. page area)
  • Landscape Orientation
  • Paper Size
  • Narrow Margins
  • Fit all columns on one page

Here is the relevant code I'm working with:

excel = win32com.client.Dispatch("Excel.Application")  # See note below 
excel.Visible = False

ewb = excel.Workbooks.Open(my_excel_file)
ews = ewb.WorkSheets[0]

## Print Selection
ews.PageSetup.PrintArea = print_area  # sring: `A1:K300`

## Landscape Orientation
# ews.PageSetup.X1Landscape

## Paper Size
# ews.PageSetup.X1PaperLetter

## Narrow Margins
# ?? I can't find this option

## Fit all columns on one page
ews.PageSetup.FitToPagesWide = 1

ewb.ActiveSheet.ExportAsFixedFormat(0, f"./Report-final.pdf")

NOTE: I'm pre-pending the variable for wb and ws with e here, because I'm also using OpenPyXl which uses the same naming pattern.

The troubles I'm having are as follows:

  1. Unless Excel is open, I get this error at the very first command:

    CRITICAL--((-2147023728, 'Element not found.', None, None))

  2. The commented code sections are not recognized:

    • ews.PageSetup.X1Landscape
      • (<unknown>.X1Landscape)
    • ews.PageSetup.X1PaperLetter
      • (<unknown>.X1PaperLetter)
  3. Remember, Excel is running (no file selected). When I run the script, the Excel file I'm converting to PDF is opened, and then I'm getting a blocking modal from Excel asking me if I want to save changes (which I assume refers to the Page Setup changes).

  4. No matter if I save or not, the script runs to completion without error, but no PDF is saved.

I'm running my script in Windows 10 in Conda (v4.8.5) using Python (v3.8.3) with PyWin32 (v227).

xtian
  • 2,449
  • 3
  • 34
  • 55

0 Answers0