python运行截图_python调用java截图

这段代码展示了如何使用Python结合Java库进行Excel截图。首先,通过openpyxl设置Excel单元格边框,然后调用Java的Aspose库对特定区域进行截图,支持多种图片格式,并将截图保存到指定目录。
摘要由CSDN通过智能技术生成

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os

import openpyxl

from openpyxl.reader.excel import load_workbook

from openpyxl.styles.borders import Border, Side

import yaml

import jpype

class ExcelToImage:

# 设置EXCEL表格为全边框

BORDER = Border(

left=Side("thin", "FF000000"),

right=Side("thin", "FF000000"),

top=Side("thin", "FF000000"),

bottom=Side("thin", "FF000000"),

vertical=Side("thin", "FF000000"),

horizontal=Side("thin", "FF000000")

)

def __init__(self, cfg):

self.file_in = cfg['file_in']

self.file_out = cfg['file_out']

self.capture_area = cfg['capture_area']

self.pic_dir = cfg['pic_dir']

self.pic_type = cfg['pic_type']

def excel_cell_border(self, file_in, capture_area, file_out):

""" 将需要截图的区域设置边框并另存为其他文件 .

:param file_in: 用于截图的EXCEL文件

:param capture_area: 截图区域 {'Sheet1': ['B2:O11']}

:param file_out: 截图区域设置边框后的输出文件

"""

'''

#案例模板

file_in = 'demo.xlsx'

capture_area = {'Sheet1': ['B2:O11', 'B17:O26']}

capture_area = {'Sheet2': ['B2:C5']}

file_out = r'D:\excel_to_img_java\datas\tmps\demo_out.xlsx'

'''

# 加载EXCEL文件

wb = openpyxl.load_workbook(file_in)

# 设置边框

for key_sheet, value_cellarea in capture_area.items():

sheet = wb[key_sheet] # 获取sheet表

for area in value_cellarea:

start, end = area.split(":")

for row in sheet[start:end]:

for cell in row:

cell.border = self.BORDER

# 另存为其他文件

wb.save(file_out)

def excel_screenshot(self, file_name, capture_area, pic_dir,pic_type):

""" 使用excel.jar包进行截图 .

:param file_name: 截图的EXCEL文件

:param areas: 截图区域

:param pic_dir: 截图存储目录

:param pic_type: 图片类型 png jpg bmp

"""

# 设置参数

#file_name = r'D:\pythoncode\my_module\my_excel_to_image\datas\tmps\报表_out.xlsx'

#capture_area = {'Sheet1': ['B2:O11', 'B17:O26', 'B31:J39', 'U2:AJ12', 'T14:AE27', 'R30:AC39', 'AN3:AW11', 'AK15:AQ27', 'AI30:AX43']}

#pic_dir = 'datas/tmps'

#pic_type = 'png'

# 调用jar处理截图

jars = "lib/excel.jar"

jpype.startJVM(jpype.getDefaultJVMPath(),"-ea", "-Xmn128m", "-Xms512M", "-Xmx512M","-Djava.class.path={0}".format(jars))

#jpype.attachThreadToJVM() # 解决多线程问题

# java初始化配置

License = jpype.JClass("com.aspose.cells.License")

l = License()

l.setLicense("lib/license.xml")

Workbook = jpype.JClass("com.aspose.cells.Workbook")

ImageFormat = jpype.JClass("com.aspose.cells.ImageFormat")

ImageOrPrintOptions = jpype.JClass("com.aspose.cells.ImageOrPrintOptions")

SheetRender = jpype.JClass("com.aspose.cells.SheetRender")

# 实例化工作簿

book = Workbook(file_name)

# 保存为html文件

#book.save("datas/out.html",12)

# 设置图片质量

imgOptions = ImageOrPrintOptions()

imgOptions.setQuality(100)

imgOptions.setOnePagePerSheet(True)

#设置分辨率

imgOptions.setHorizontalResolution(800)

imgOptions.setVerticalResolution(600)

# 设置图片格式

if pic_type == 'jpg':

imgOptions.setImageFormat(ImageFormat.getJpeg())

elif pic_type == 'png':

imgOptions.setImageFormat(ImageFormat.getPng())

elif pic_type == 'bmp':

imgOptions.setImageFormat(ImageFormat.getBmp())

CalculationOptions = jpype.JClass(

"com.aspose.cells.CalculationOptions")

opt = CalculationOptions()

sheet_ct = 1

for dic_key, dic_value in capture_area.items():

# 获取sheet

sheet = book.getWorksheets().get(dic_key)

# 对sheet中的公式进行计算

sheet.calculateFormula(opt, True)

# 设置区域

pageSetup = sheet.getPageSetup()

# 去掉边距

pageSetup.setBottomMargin(0.)

pageSetup.setLeftMargin(0.)

pageSetup.setRightMargin(0.)

pageSetup.setTopMargin(0.)

# 区域

for index, area in enumerate(dic_value):

pageSetup.setPrintArea(area)

sr = SheetRender(sheet, imgOptions)

for page in range(sr.getPageCount()):

# Generate an image for the worksheet

if index > 8: # 比如index = 9

index = index * 10 # 9*10 = 90 +1 ==91,92,93

sr.toImage(page, os.path.join(pic_dir, "%d.%s" %(sheet_ct*10+(index+1),pic_type)))

sheet_ct = sheet_ct + 1

jpype.shutdownJVM() # 不要关闭否则第二次无法启动

def run(self):

# 1.获取参数

file_in = self.file_in

file_out = self.file_out

capture_area = self.capture_area

pic_dir = self.pic_dir

pic_type = self.pic_type

# 2.对截图Sheet设置边框,并另存为文件

#self.excel_cell_border(file_in, capture_area, file_out)

# 3.对指定区域截图

self.excel_screenshot(file_in, capture_area, pic_dir,pic_type)

if __name__ == '__main__':

cfg = {}

cfg["file_in"] = 'data_report.xlsx'

cfg["file_out"] = 'datas/tmps/data_report_out.xlsx'

cfg["capture_area"] = {'Sheet1': ['B10:P36']}

cfg["pic_dir"] = 'datas/tmps'

cfg["pic_type"] = 'png'

ss = ExcelToImage(cfg)

ss.run()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值