-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexp16b.py
56 lines (42 loc) · 2.8 KB
/
exp16b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from head import * # 导入万能头
def name(): # 返回实验名称
return "LED的发光强度特性"
def handle(workpath,extension):
# 处理数据并生成文档,workpath为工作文件夹路径(本程序涉及到的所有文件只能保存在此文件夹内),extension为扩展名(csv/xls/xlsx)
try:
zhfont = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Regular.otf") # 设置图像中的文字字体
excelpath=workpath+name()+'.'+extension # Excel文件名(含路径),文件名与name()函数返回值一致
if extension=='csv':
with open(excelpath,'rb') as f:
encode=chardet.detect(f.read())["encoding"] # 判断编码格式
data=pd.read_csv(excelpath, header=0, names=["RI","RU","GI","GU","BI","BU"], encoding=encode) # 读取csv文件
else:
data=pd.read_excel(excelpath, header=0, names=["RI","RU","GI","GU","BI","BU"]) # 读取xls/xlsx文件
os.remove(excelpath) # 读取Excel数据后删除文件
fig, ax = plt.subplots() # 新建绘图对象
ax.xaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator(2))
ax.yaxis.set_minor_locator(matplotlib.ticker.AutoMinorLocator(2))
# 设置副刻度为主刻度的一半
ax.plot(data["RI"], data["RU"], color='r', linewidth=1.5, marker="o", markersize=3, label = "R")
ax.plot(data["GI"], data["GU"], color='g', linewidth=1.5, marker="o", markersize=3, label = "G")
ax.plot(data["BI"], data["BU"], color='b', linewidth=1.5, marker="o", markersize=3, label = "B")
ax.legend()
# 作图,详见 https://www.runoob.com/matplotlib/matplotlib-marker.html 和 https://www.runoob.com/matplotlib/matplotlib-line.html
ax.set_title("LED发光强度曲线", fontproperties=zhfont) # 若有中文,需加fontproperties=zhfont
ax.set_xlabel("电流I/mA", fontproperties=zhfont)
ax.set_ylabel("相对光强L(光电池电压U/mV)", fontproperties=zhfont)
# 添加标题和轴标签,详见 https://www.runoob.com/matplotlib/matplotlib-label.html
imgpath=workpath+"img.jpg"
fig.savefig(imgpath, dpi=300, bbox_inches='tight')
plt.close()
docu=Document()
docu.styles['Normal'].font.name = '微软雅黑'
docu.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') # 设置Word文档字体
docu.add_paragraph(name()) # 在Word文档中添加文字
docu.add_picture(imgpath)
docu.save(workpath+name()+".docx") # 保存Word文档,注意文件名必须与name()函数返回值一致
os.remove(imgpath) # 删除刚才保存的图像
return 0 # 若成功,返回0
except:
traceback.print_exc() # 打印错误
return 1 # 若失败,返回1