二级域名网站怎么投广告,网站开发岗位职责,宁波网络推广专员,泰国做彩票网站win32com库基于wps对Word文档的基础操作 文章目录 win32com库基于wps对Word文档的基础操作新建/打开文档段落操作(Paragraph)字体设置(Font)图表操作(Shape) 参考链接:
WAS API手册 新建/打开文档
import win32com
import win32com.client as win32
# 启动WPS进程
word_obj …win32com库基于wps对Word文档的基础操作 文章目录 win32com库基于wps对Word文档的基础操作新建/打开文档段落操作(Paragraph)字体设置(Font)图表操作(Shape) 参考链接:
WAS API手册 新建/打开文档
import win32com
import win32com.client as win32
# 启动WPS进程
word_obj win32.gencache.EnsureDispatch(KWPS.Application)
word_obj .Visible 1
# 打开文件
dst_file 你的文档路径
dst_doc word_obj.Documents.Open(dst_file)
# 新建文档 返回一个空白文档对象
dst_doc word_obj.Documents.Add(dst_file)段落操作(Paragraph)
# Paragraphs 返回所选内容、范围或文档中所有的段落的集合
# 获取全部段落
dst_Paragraphs dst_doc.Paragraphs
# 获取第1段
dst_Paragraph dst_doc.Paragraphs(1)# 段落方法
dst_Paragraph.Delete() # 删除段落dst_Paragraph.Select() # 选中段落# 设置段落水平对齐方式为居中对齐 其余对齐方式参考官方文档中的枚举变量 WdParagraphAlignment
dst_Paragraph.Alignment 1# 设置段落垂直对齐方式为中心对齐 其余对齐方式参考官方文档中的枚举变量 WdBaselineAlignment
dst_Paragraph.BaseLineAlignment 1# 设置段落段前、段后间距
dst_Paragraph.LineUnitBefore 1 # 段前 单位 行
dst_Paragraph.LineUnitAfter 1 # 段后 单位 行
dst_Paragraph.SpaceBefore 12 # 段前 单位 磅
dst_Paragraph.SpaceAfter 12 # 段后 单位 磅# 设置段落大纲级别 # 若段落样式为标题样式则无法修改
dst_Paragraph.OutlineLevel 1# 段落左缩进和右缩进
dst_Paragraph.CharacterUnitLeftIndent 0 # 左缩进 单位 字符
dst_Paragraph.CharacterUnitRightIndent 0 # 右缩进 单位 字符
dst_Paragraph.LeftIndent 0 # 左缩进 单位 磅
dst_Paragraph.RightIndent 0 # 右缩进 单位 磅# 段落首行缩进、悬挂缩进 为正值时表示首行缩进,负值时表示悬挂缩进
dst_Paragraph.CharacterUnitFirstLineIndent 2 #单位 字符
dst_Paragraph.FirstLineIndent 12 #单位 磅# 设置段落行距为单倍行距 其余行距类型参考官方文档中的枚举变量 WdLineSpacing
dst_Paragraph.LineSpacingRule 0# 若设置段落行距类型为固定值或多倍行距时 需要单独设置 LineSpacing属性
dst_Paragraph.LineSpacing 12 # 其中一行等于12磅 或者使用 word_obj.LinesToPoints(1)进行计算WPS中各单位转换为磅值关系及方法如下:
1行 12磅 LinesToPoints()
1cm 28.35磅 CentimetersToPoints()
1毫米 2.85磅 MillimetersToPoints()
1英寸 72磅 InchesToPoints()
像素 -- 磅 PixelsToPoints()字体设置(Font)
Paragraph dst_doc.Paragraphs(1) # 获取第一段Range 表示对象在word文档中的连续区域# 设置段落字体中文样式
Paragraph.Range.Font.NameFarEast 楷体_GB2312 # 设置中文字体
# 设置段落字体英文样式 其余样式详见开发文档
Paragraph.Range.Font.Name Times New Roman
# 复杂文本
Paragraph.Range.Font.NameAscii Times New Roman
# 设置字体大小
Paragraph.Range.Font.Size 18 # 单位 磅 小二为18磅
# 设置字号
Paragraph.Range.Font.SizeFarEast 3 #设置字体为3号# 字体加粗
Paragraph.Range.Font.Bold True
# 字体倾斜
Paragraph.Range.Font.Italic True
# 设置字符间距
Paragraph.Range.Font.Spacing 1图表操作(Shape)
# 获取第一个图表对象
dst_shape dst_doc.Shapes(1)
# 设置大小
dst_shape.Height 128 # 高
dst_shape.Width 128 # 宽# 设置文字环绕
dst_shape.WrapFormat.Type win32com.client.constants.wdWrapSquare # 四周型 (wdWrapSquare)
dst_shape.WrapFormat.Side win32com.client.constants.wdWrapBoth # 环绕文字两边
# 设置四周边距
dst_shape.WrapFormat.DistanceTop 0 # 上0厘米
dst_shape.WrapFormat.DistanceBottom 0 # 下0厘米
dst_shape.WrapFormat.DistanceLeft self.word.CentimetersToPoints(0.32) # 左0.32厘米
dst_shape.WrapFormat.DistanceRight self.word.CentimetersToPoints(0.32) # 右0.32厘米
# 设置位置为页边距
dst_shape.RelativeHorizontalPosition 0 # 1代表页边距
dst_shape.RelativeVerticalPosition 0 # 1代表页边距
dst_shape.Top 0
dst_shape.Left 0
dst_shape.WrapFormat.AllowOverlap False # 不允许重叠
嵌入式图表操作(InlineShape)
表格处理(Table、Row、Column、Cell)
dst_table dst_doc.Tables(1)
# 获取行|列数
row_count dst_table.Rows.Count
col_count dst_table.Columns.Count
# 设置行高 单位 磅
dst_table.Rows(1).Height 20
# 设置列宽
dst_table.Columns(1).Width 20
# 设置表格的所有行的行高
dst_table.Rows.Height 20# 设置单个单元格的宽高 PS设置时会自动修改其所在行和列的行高和列宽
dst_table.Cell(1,1).Height 20
dst_table.Cell(1,1).Width 20# 设置表格文本样式
dst_table.Cell(1,1).Range.Text 设置文本
# 设置字体样式(其余属性请参考Font设置)
dst_table.Cell(1,1).Font.Name
# 调整单元格边距
dst_table.Rows(1).TopPadding 20 # 上边距
dst_table.Rows(1).BottomPadding 20 # 下边距
dst_table.Rows(1).LeftPadding 20 # 左边距
dst_table.Rows(1).RightPadding 20 # 右边距