私はこれを自分で行う方法を見つけたので、私がどのようにしたのか投稿したいと思っていました。このソリューションは、PythonルーチンからカスタムExcelマクロを起動します。
私が必要な最初の事はダミーを作成することでした/空白のExcelは、私がSelection_Printer.xlsm
鉱山と呼ばれる(.xlsm拡張子を持つファイル次に、そのExcelファイルでのVBAモジュールの1つに、私は次のコードを書きました:。
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
のパスの場所にminRow
、maxRow
、minCol
、およびワークシート内maxCol
によって定義されたセル範囲を印刷
私の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")