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

How To: Create A Word Document In Powershell – Part 1 – Opening The Document, Writing Some Text, Usi

If you’re in the business of extracting data from disparate systems and presenting that in reports, then you’ll know how time-consuming (and frankly boring) it can be to format that data into tables or formats suitable for said report. Well, if your data is accessible via Powershell then this is the set of blogs for you! I’ll be writing a multi-part series on creating and manipulating Word documents in Powershell so that you can start to automate those tedious tasks and improve the consistency of your documents!


Table of Contents

  1. Opening the Word Document, Writing Some Text, Using Styles
  2. Adding and Manipulating Tables, Adding Images
  3. Document Properties, Table of Contents, Changing Page Properties
  4. Moving around the Document

Use a Template, or create everything on the fly?

It can be tempting, once you get to the end of this blog, to think“Ah great, now I can create an entire Word document directly in Powershell”and I get that completely – I had the same thought the first time I started working with this. However, there are two big reasons why I’d advise against it:

  1. It’s an awful lot of Powershell – adding all of the repeatable pieces of your document as part of the Powershell can make your script look very busy, and any modifications require you to pick through the Powershell and find the specific piece you need to update
  2. You end up repeating a lot of code! We should all be trying to reduce the amount of repeated code in our scripts; it’s inefficent, and making changes to one piece of code requires you to change all like pieces of code to maintain consistency.

Because of those reasons, I prefer to start with a templated file that sets the key components that are in every document created using the process – a title page, the table of contents, maybe (depending on the use case) text that is consistent regardless of the specifics in the document. Therefore, although I might add examples below on adding elements I include in templates the bulk will be assuming you’re also using a template.

Opening the Word document

Before we can do anything else, we need to open Word and the document we want to modify. All manipulation with word uses theWord.ApplicationComObject and so we don’t need to install or import any modules.

$word = New-Object -ComObject Word.Application #If you're starting with a blank document $doc = $word.Documents.Add() #If you're starting with a templated document $doc = $word.Documents.Open([Path to File])

TIP: If you want to see the changes that are being made to the document live, then just add this line as well:

$word.visible = $true

Setting the “Cursor”

Now that we’ve got our Word document and it’s open, we need to be able to start creating text inside it – and just like working with Word traditionally, we need to set our “cursor” location.

There are a number of different ways you can do this, depending on the complexity of your requirements and if you’re working with a template or not, but for the purpose of this we’re going to assume you want to start at the beginning of the document – and so we’re going to create our cursor at the default location:

$text = $word.Selection

Styles and Text

The$textobject is going to be our most used on throughout this series, as we move our “cursor” through the document, and below you can see it in action:

$text.style = "Heading 1" $text.TypeText("This is Heading 1") $text.TypeParagraph() $text.style = "Body Text" $text.TypeText("This is a section of text immediately under Heading 1. This might be where you add some dynamically generated information") $text.TypeParagraph() $text.style = "Heading 2" $text.TypeText("This is Heading 2") $text.TypeParagraph()

There’s a couple of different things going on here, so let me explain each property:

  • $text.stylesets the style of the current selection to one of your existing predefined styles; I’ve chosen to use the default ones you get in Word, but you may have your own pre-defined styles to use – if so, just use the name of them.
  • $text.TypeTextdoes what it suggest it does – it types text! Just enter whatever text you want to be typed into the page here, and it will go ahead and do it.
  • $text.TypeParagraphis the same as pressing Enter or Return on your keyboard; it will start a new paragraph

Before I start typing text, I explicitly set the Style – this will depend on your specific styles and whatever the ‘Style to use next’ is set to, but I like to do this as a method of ensuring the document is styled exactly how I expect it to.

And that’s it! You’ll now have the beginnings of a Word document. In the next part where we tackle tables I’ll also show how you might use that to present information collected from an external source, such as an API. Below is a full script to achieve everything I’ve talked about in this post – make sure you keep a hold of this, as you’re going to be using it in the following parts:

$word = New-Object -ComObject Word.Application $word.visible = $true #If you're starting with a blank document $doc = $word.Documents.Add() $text = $word.Selection $text.style = "Heading 1" $text.TypeText("This is Heading 1") $text.TypeParagraph() $text.style = "Body Text" $text.TypeText("This is a section of text immediately under Heading 1. This might be where you add some dynamically generated information") $text.TypeParagraph() $text.style = "Heading 2" $text.TypeText("This is Heading 2") $text.TypeParagraph() $doc.SaveAs([Path To Save File To]) $doc.Close $word.Quit
http://www.gsyq.cn/news/1610723.html

相关文章:

  • Highcharts有版权吗?
  • TVA在具身智能全栈能力体系中的关键作用(6)
  • VMware虚拟机安装Ubuntu Linux:从零搭建开发环境的完整指南
  • 北方高寒矿区专网通信搭建要点,适配低温、粉尘、防爆严苛工况
  • 基于YOLOv8的船舶检测分类系统:从模型训练到部署的完整实践
  • 如何用Taskt实现零代码办公自动化:免费RPA工具完整指南
  • 3分钟终极指南:为Windows免费换上macOS专业鼠标指针
  • 极低成本 AI 服务:独立开发者的多模型混合路由与流量网关设计
  • AI渐进编程之五:给 Agent 穿上动力装甲——SIADOS 状态转移方法
  • 告别云服务器!用旧手机+Debian+AidLux,5分钟搭建你的移动AI开发环境
  • Claude Code从入门到精通(3)-settings.json 与 CLAUDE.md
  • XCOM 2模组管理器终极指南:如何用AML告别卡顿与冲突
  • Platinum-MD:如何让20年前的MiniDisc设备在现代电脑上重获新生?
  • JDBC基础(2)
  • 想提升用户体验?快把HTML5视频播放器代码嵌入你的网站
  • 抖音视频下载神器:轻松保存无水印高清内容
  • PC大型3A 角色扮演游戏(RPG)《怪物猎人物语3:命运双龙》网盘下载 免BIOS 中文版
  • 阿里云图像搜索完整对接指南:从开通到API/SDK深度集成
  • G-Helper:华硕笔记本的轻量级控制中心,三步告别臃肿系统
  • Mac 新手必装工具清单:从效率、安全到清理维护的完整指南(2026 更新版)
  • LabVIEW VISA异步I/O提升吞吐量
  • puzzle(1131)指路罗马
  • 3分钟彻底解决Windows和Office激活难题:KMS_VL_ALL_AIO完整操作指南
  • YOLO26N 姿态估计 RKNN 部署:RK3588 NPU 实战
  • 扣子【Coze】实战:别再花钱买绘本了!用扣子一键生成,孩子天天要看新故事
  • 基于 Simulink 的工业离心机变频调速系统 S-Ramp(S型加减速)曲线规划仿真实战教程。
  • JMeter 实现:上接口失败则不执行下一个接口
  • JavaScript的DOM操作基础
  • docker python images Docker Python镜像别乱拉!容器和镜像傻傻分不清,你还在踩坑?
  • YOLO26N 姿态估计 TensorRT 部署:Jetson 实时推理