为使工作时,写测试报告更加简单,编写了该程序。
由于工作时,跨域电路要写本地、全程、点到点测试报告,很麻烦,所以编写了该程序,替代手工书写,提高了工作效率。
代码如下:
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import numpy as np
import matplotlib.pyplot as plt
import xlrd
import os
# 读取EXCEL
line = 95
line = line - 1
page = 1
excelPath = "交付单.xlsx"
book = xlrd.open_workbook(excelPath)
sheet = book.sheets()[page]
guestName = sheet.row_values(line)[7] # 用户名称
circuitCode = sheet.row_values(line)[11] # 用户电路代号
bandwidth = sheet.row_values(line)[10] # 用户电路速率
index = bandwidth.index('M')
bandwidth = bandwidth[:index]
aEndAddress = sheet.row_values(line)[12] # 用户A端地址
zEndAddress = sheet.row_values(line)[13] # 用户Z端地址
testDate = xlrd.xldate.xldate_as_datetime(sheet.cell(line, 17).value, 0).strftime("%Y-%m-%d") # 测试日期
date = testDate # 日期
# 图片写入文字
# 本地
imagePath = './本地测试报告.jpg'
img = Image.open(imagePath)
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("C:\Windows\Fonts\simsun.ttc", 30)
color = (0, 0, 0)
draw.text((546, 337), guestName, color, font=font)
draw.text((546, 423), circuitCode, color, font=font)
draw.text((1311, 498), bandwidth, color, font=font)
maxNum = 32
if len(aEndAddress) > maxNum:
aEndAddress_1 = aEndAddress[:maxNum]
aEndAddress_2 = aEndAddress[maxNum:]
draw.text((546, 564), aEndAddress_1, color, font=font)
draw.text((546, 599), aEndAddress_2, color, font=font)
else:
draw.text((546, 564), aEndAddress, color, font=font)
if len(zEndAddress) > maxNum:
zEndAddress_1 = zEndAddress[:maxNum]
zEndAddress_2 = zEndAddress[maxNum:]
draw.text((546, 663), zEndAddress_1, color, font=font)
draw.text((546, 696), zEndAddress_2, color, font=font)
else:
draw.text((546, 663), zEndAddress, color, font=font)
draw.text((1124, 1660), testDate, color, font=font)
draw.text((1180, 2082), date, color, font=font)
img = np.array(img)
plt.imshow(img, cmap=plt.cm.binary)
#plt.show()
# 全程
imagePath = './全程测试报告.jpg'
img1 = Image.open(imagePath)
draw = ImageDraw.Draw(img1)
font = ImageFont.truetype("C:\Windows\Fonts\simsun.ttc", 30)
draw.text((546, 337), guestName, color, font=font)
draw.text((546, 423), circuitCode, color, font=font)
draw.text((1311, 498), bandwidth, color, font=font)
if len(aEndAddress) > maxNum:
aEndAddress_1 = aEndAddress[:maxNum]
aEndAddress_2 = aEndAddress[maxNum:]
draw.text((546, 564), aEndAddress_1, color, font=font)
draw.text((546, 599), aEndAddress_2, color, font=font)
else:
draw.text((546, 564), aEndAddress, color, font=font)
if len(zEndAddress) > 33:
zEndAddress_1 = zEndAddress[:maxNum]
zEndAddress_2 = zEndAddress[maxNum:]
draw.text((546, 663), zEndAddress_1, color, font=font)
draw.text((546, 696), zEndAddress_2, color, font=font)
else:
draw.text((546, 663), zEndAddress, color, font=font)
draw.text((1124, 1660), testDate, color, font=font)
draw.text((1180, 2082), date, color, font=font)
img1 = np.array(img1)
plt.imshow(img1, cmap=plt.cm.binary)
#plt.show()
# 点到点
imagePath = './点到点测试报告.jpg'
img2 = Image.open(imagePath)
draw = ImageDraw.Draw(img2)
font = ImageFont.truetype("C:\Windows\Fonts\simsun.ttc", 30)
draw.text((546, 337), guestName, color, font=font)
draw.text((546, 423), circuitCode, color, font=font)
draw.text((1311, 498), bandwidth, color, font=font)
if len(aEndAddress) > maxNum:
aEndAddress_1 = aEndAddress[:maxNum]
aEndAddress_2 = aEndAddress[maxNum:]
draw.text((546, 564), aEndAddress_1, color, font=font)
draw.text((546, 599), aEndAddress_2, color, font=font)
else:
draw.text((546, 564), aEndAddress, color, font=font)
if len(zEndAddress) > 33:
zEndAddress_1 = zEndAddress[:maxNum]
zEndAddress_2 = zEndAddress[maxNum:]
draw.text((546, 663), zEndAddress_1, color, font=font)
draw.text((546, 696), zEndAddress_2, color, font=font)
else:
draw.text((546, 663), zEndAddress, color, font=font)
draw.text((1124, 1660), testDate, color, font=font)
img2 = np.array(img2)
plt.imshow(img2, cmap=plt.cm.binary)
#plt.show()
# 保存
# 创建文件夹
# 文件夹名字:序号.工单标题_资源流水号_产品编码_电路编码
num = 3
title = sheet.row_values(line)[2] + "_"
serialNum = sheet.row_values(line)[3][:-2] + "_"
proCode = sheet.row_values(line)[6] + "_"
circuitCode = sheet.row_values(line)[11]
dir = str(num) + '.' + title + serialNum + proCode + circuitCode
dir = "D:\PycharmProjects\study\Python" + "\\" + dir
os.mkdir(dir)
# 文件夹里面存文件
plt.imsave(dir + '\\' + '本地测试报告.jpg', img)
plt.imsave(dir + '\\' + '全程测试报告.jpg', img1)
en2EnTitle = '点到点测试报告-' + title + serialNum + proCode + circuitCode + '.jpg'
plt.imsave(dir + '\\' + en2EnTitle, img2)
该程序有效地提高了写测试报告的效率。