当前位置: 首页 > news >正文

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

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 # 右边距
http://www.lryc.cn/news/482859.html

相关文章:

  • Kubernetes 网络之深度探索:网络模型与 CNI 插件
  • Go 模块管理教程:go.mod 与依赖版本控制
  • 大数据 ETL + Flume 数据清洗 — 详细教程及实例(附常见问题及解决方案)
  • 鸿蒙next版开发:订阅应用事件(ArkTS)
  • F litter 开发之flutter_local_notifications
  • springboot参数校验
  • Spring生态学习路径与源码深度探讨
  • C++:set详解
  • (一)- DRM架构
  • Docker了解
  • 【DL】YOLO11 OBB目标检测 | 模型训练 | 推理
  • vue读取本地excel文件并渲染到列表页面
  • github 以及 huggingface下载模型和数据
  • 使用 Vue 配合豆包MarsCode 实现“小恐龙酷跑“小游戏
  • 51c视觉~合集6
  • STM32(hal库)在串口中,USART和uart有什么区别?
  • 机器学习、深度学习面试知识点汇总
  • FPGA高速设计之Aurora64B/66B的应用与不足的修正
  • 如何通过PHP脚本自动推送WordPress文章至百度站长平台
  • ORA-01092 ORA-14695 ORA-38301
  • upload-labs通关练习---更新到15关
  • WPF 应用程序中使用 Prism 框架时,有多种方式可以注册服务和依赖项
  • 【ESP32】ESP-IDF开发 | 低功耗管理+RTC唤醒和按键唤醒例程
  • Windows 局域网IP扫描工具:IPScaner 轻量免安装
  • HTML的浮动与定位
  • 【网络安全 | 漏洞挖掘】我如何通过路径遍历实现账户接管
  • DB-GPT系列(四):DB-GPT六大基础应用场景part1
  • SpringCloud篇(服务拆分 / 远程调用 - 入门案例)
  • Rust 建造者模式
  • ANN DNN CNN SNN