win32com库基于wps对Word文档的基础操作

win32com库基于wps对Word文档的基础操作

文章目录

参考链接: WAS API手册

新建/打开文档

python 复制代码
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)

python 复制代码
# 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)

python 复制代码
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)

python 复制代码
# 获取第一个图表对象
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 # 右边距
相关推荐
游乐码9 小时前
c#变长关键字和参数默认值
学习·c#
全栈小510 小时前
【C#】合理使用DeepSeek相关AI应用为我们提供强有力的开发工具,在.net core 6.0框架下使用JsonNode动态解析json字符串,如何正确使用单问号和双问号做好空值处理
人工智能·c#·json·.netcore·deepseek
wearegogog12310 小时前
基于C#的TCP/IP通信客户端与服务器
服务器·tcp/ip·c#
A_nanda15 小时前
C#调用Quartz.NET的完整实现。
c#·.net·quartz
2501_9307077816 小时前
使用C#代码在 PowerPoint 演示文稿中插入表格
开发语言·c#·powerpoint
少控科技17 小时前
C#基础训练营 - 01 - 数据类型
开发语言·c#
1314lay_100717 小时前
Vue3 + Element Plus项目和C# .Net 7.0 Core后端API项目发布部署到服务器
服务器·前端·javascript·vue.js·elementui·c#·.net
郝亚军17 小时前
c#如何编译、通过icd文件生成static_model.c和static_model.h
开发语言·c#
Traced back18 小时前
保姆级C#进阶教程:从入门到企业级开发,小白也能秒懂!
开发语言·c#
柒儿吖18 小时前
CharLS 无损 JPEG-LS 库在 OpenHarmony 的 lycium 适配与 ctest 验证
c++·华为·c#·harmonyos