【源码】reportgen:Matlab自动生成WORD/PPT的实用技巧

reportgen
本文将介绍Matlab的一个实用技巧。一键生成Word版本的报告。Matlab之前都是通过调用COM接口来实现的,类似于VBA,虽然可做的事情很多,但并不适合一般的童鞋。

  1. 自动化报告
    测试平台: MATLAB 2015b

先来看一个最简单的例子(demo1.m)

% demo1.m
import mlreportgen.dom.;
d = Document(‘demo1’,‘docx’);
open(d);
append(d,‘hello world!’);
close(d);
rptview(d.OutputPath);
import mlreportgen.dom.
;
第一行是将所需要的类和函数导入工作空间。这里我们最常用的类就是Document.

d = Document(‘test’,‘docx’);
open(d);
在第二、三行中我们新建一个文档,其中文件类型docx可以替换成html或者pdf. Document类的相关属性可以通过命令查看 (doc mlreportgen.dom.Document)

append(d,‘hello world!’);
在第四行我们给文档添加了一句经典的话,我们还可以自定义它们的样式。比如加粗、斜体、改成蓝色、字体大小等等。

% 替换上面的一行代码
p=Text(‘hello world’);
p.Style={Bold(true),FontSize(‘16pt’),Color(‘blue’)};
p.Strike=‘double’;
append(d,p)
这里出现了第一个文档类:Text,即文本。Matlab提供了很多可调节的属性,如下表。修改起来也很方便,如要把文本改为斜体,则添加:p.Italic=1;即可。

Text的属性 取值 备注
Bold true/false 加粗
Color str(‘red’、'blue’等) 颜色
Italic true/false 斜体
BackgroundColor str 背景色
Underline str(‘single’、'dotdash’等) 下划线
FontFamilyName str 字体
FontSize str(‘12pt’) 字体大小
Strike str 删除线
StyleName str 模板中的样式名
接下来我们将生成一个更复杂的文档。首先想想一个word文档一班都有哪些元素。MATLAB提供了很多(下面的表格仅列出了部分)。本文主要介绍其中的五个:页面设置、标题、段落、表格、图片。

属性 含义
CustomAttribute Custom element attribute
CustomElement Custom element of a document
CustomText Plain text to be appended to a custom element
DOCXPageFooter Page footer for a Word document.
DOCXPageHdrFtr Page Base class for page header and footer
DOCXPageHeader Page header for a Word document.
DOCXPageMargins Margins of pages in a Word page layout
DOCXPageSize Size, orientation of pages in a Word layout
DOCXSection Page layout section of a Word document
Document Create a dom document
ExternalLink Create a hyperlink to an external target
Form Defines a form
FormalTable Create a formal table
Group Group of document objects
Heading Create a heading paragraph.
HorizontalRule Create a horizontal rule.
Image Create an image to be included in a report.
ImageArea Defines an image area as a hyperlink
InternalLink Create a hyperlink to a target in this
LinkTarget Create a target for a hyperlink.
ListItem Item in a list
Node Defines a document node
Object Defines a document object
OrderedList Ordered (numbered) list
Paragraph Create a formatted block of text, i.e., a paragraph.
Table Create a table.
TableColSpecGroup Defines style of a group of table columns
TableEntry Create a table Entry
TableRow Creates a table row
Template Create a template for a document
Text Create a text object
接下来我们做一个完整的模板。首先新建空白文档。

% demo2.m
import mlreportgen.dom.*;
d=Document(‘demo2’,‘docx’);
open(d);
1.1 页面
页面设置包括页面大小('A4’等)、页面方向、页边距.

% 页面设置
s = d.CurrentDOCXSection;
s.PageSize.Orientation =‘landscape’; % portrait(default)
s.PageSize.Height = ‘8.5in’;
s.PageSize.Width = ‘11in’;
s.PageMargins.Left = ‘3.0cm’;
s.PageMargins.Right = ‘3.0cm’;
s.PageMargins.Top = ‘2.5cm’;
s.PageMargins.Bottom = ‘2.5cm’;
% 中文字体样式设置
heiti=FontFamily;
heiti.FamilyName=‘Arial’;
heiti.EastAsiaFamilyName=‘黑体’;
songti=FontFamily;
songti.FamilyName=‘Arial’;
songti.EastAsiaFamilyName=‘宋体’;
1.2 标题
标题的初始格式由模板中自带的标题样式决定,当然也可以自己修改。

%% 标题
p=Heading(1,‘Matlab 自动化报告模板’);% 一级标题
%p.Color=‘red’;
%p.HAlign=‘center’;
p.Style={heiti,Color(‘red’),HAlign(‘center’)};
append(d,p);
1.3 段落
与文本不同,段落除下文本的属性外,还有边距、对齐、首行缩进、行间距、段前、段后等属性需要调节。在下面的代码框中,FirstLineIndent代表首行缩进的宽度,LineSpacing代表行间距,OuterMargin代表边距。在这里我们还引入了一个新的类:ExternalLink(外部链接),当然Matlab也提供文档内部链接:InternalLink.

%% 段落
append(d,Heading(2,‘一、段落模板’));
s=‘这里是段落。’;
s=repmat(s,[1,12]);
p = Paragraph(s);
% 中文字体样式自定义
p.Style={songti,Color(‘blue’),…
LineSpacing(1.5),…
OuterMargin(‘10pt’,‘0pt’,‘0pt’,‘10pt’)};
p1=Text(‘下划线。’);%同段落差不多.
p1.Underline=‘single’;
p1.Color=‘red’;
append(p,p1);
append(p,s);
p2=ExternalLink(‘http://github.com/gasongjian/’, ‘超链接’);
append(p,p2);
p.FontSize=‘14pt’;
p.FirstLineIndent=‘28pt’;%这里差不多就是2个字符大小
append(d,p);

完整资料领取请加QQ群免费下载:在这里插入图片描述

  • 4
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值