获取 Python 到 pdf/image 在 MS Excel 中打印选定的单元格

Get Python to pdf/image print selected cells in MS Excel

在 python 程序中,我需要从 Excel 文件中捕获 select 细胞离子作为图像。现在,我的解决方案很复杂,涉及使用 win32com 将 Excel 文件另存为 PDF,然后使用图像打印机创建 PDF 图像,然后裁剪所需区域。问题是有时所需单元格的位置在保存的 PDF 中会稍微移动,这使得 accurately/repeatably 无法在图像中裁剪所需的单元格。

我想select Excel 中的一系列单元格,如下所示

然后我想使用 win32com 从 Python 打印那些 selected 单元格(我不是说打印到屏幕,而是打印到 OS 中配置的打印机),或类似的。这可能吗?如果是这样,请提供一些示例代码,或 link 教程等

Excel 在其打印选项中有此 "Print Selection" 选项(见下文)。

如果可能的话,我可以使用我安装的图像打印机将 select 细胞直接打印成图像。

好吧,我自己找到了一种方法,所以我想我会 post 我是怎么做的。该解决方案从 Python 例程启动自定义 Excel 宏。

我需要做的第一件事是创建一个扩展名为 .xlsm 的 dummy/blank Excel 文件(我将我的命名为 Selection_Printer.xlsm。然后在其中一个 VBA在 Excel 文件中的模块我编写了以下代码:

Sub PrintSelection(wbPath As String, minRow As Integer, maxRow As Integer, minCol As Integer, maxCol As Integer)

    Dim wb As Workbook, sh As Worksheet

    'Get path of this file
    'myPath = Application.ThisWorkbook.Path

    'Get the workbook objects needed
    Set wb = Workbooks.Open(wbPath)

    'Get the worksheet objects needed
    'All of the sheets I wanted to print were "Sheet1", 
    '  but another parameter could be added to make this dynamic
    Set sh = wb.Sheets("Sheet1")

    'Select the cells desired
    Call SelectRange(sh, minRow, maxRow, minCol, maxCol)

    'Print the selection only
    Selection.PrintOut From:=1, To:=1, Copies:=1, Collate:=True

End Sub

Sub SelectRange(sh As Worksheet, minRow As Integer, maxRow As Integer, minCol As Integer, maxCol As Integer)

    Dim selRange As Range

    With sh
      .Cells(minRow, maxCol).Select 'start by selecting upper left corner
      Set selRange = .Range(.Cells(minRow, minCol), .Cells(maxRow, maxCol))
    End With

    selRange.Select

End Sub

这将在 wbPath 的路径位置打印工作表中由 minRowmaxRowminColmaxCol 定义的单元格区域。

在我的 Python 代码中,我使用以下代码调用此宏:

import os    
from win32com import client
import win32print

def excel_jpg_macro(xl_path, row_min, row_max, col_min, col_max):
    """
    Program that calls the selection printing Macro in Excel from Python

    :param xl_path: Full path to XL file that has cell selection to be printed
    :param row_min: starting row of the selection desired (Excel uses indexing 
                    starting at 1) as an integer
    :param row_max: ending row of the selection desired as an integer
    :param col_min: starting column of the selection desired as an integer
    :param col_max: ending column of the selection desired as an integer
    """

    print_path = os.path.abspath('./Selection_Printer.xlsm')
    xl_macro = 'PrintSelection'
    # Use win32 to run the Excel Macro from Python
    xl = client.Dispatch("Excel.Application")
    xl.Workbooks.Open(Filename=print_path, ReadOnly=True)
    xl.Application.Run(xl_macro, xl_path, row_min, row_max, col_min, col_max)
    xl.Application.Quit() 
    del xl


def jpg_excel_selection(xl_path, selection, save_name):
    """
    Prints a selection from an excel file to a jpg image printer
    :param xl_path: Full OS path to the Excel file that you want to print from
    :param selection: tuple with the (minimum row, maximum row, minimum column,
                      maximum column) with each value inside the tuple as an 
                      integer.  NOTE: Excel uses indexing starting at 1
    """

    try:
        # Capture the current default printer
        curr_printer = win32print.GetDefaultPrinter()

        # Temporarily change the default printer to be the image printer
        temp_printer = "ImagePrinter Pro"
        win32print.SetDefaultPrinter(temp_printer)

        # Run the macro that prints just the selection
        xl_path = os.path.abspath(xl_path)
        excel_jpg_macro(xl_path, *selection)

        # Change the default printer back to the original setting
        win32print.SetDefaultPrinter(curr_printer)
    except:
        print("Printing XL Selection Image Failed")