qq_37156213 2023-03-29 10:13 采纳率: 53.3%
浏览 97
已结题

PowerShell如何操作记事本?

PowerShell如何操作记事本?比如:
①往记事本读写内容
②设置打印的页面边距,页眉页码格式。
诚信第一,测试问题解决秒采纳,否则不采纳!

  • 写回答

11条回答 默认 最新

  • 小逸2023 2023-03-29 10:25
    关注
    该回答引用ChatGPT
    PowerShell可以通过COM对象来操作记事本,具体步骤如下:
    1. 创建记事本应用程序对象
    powershell
    $notepad = New-Object -ComObject "Word.Application"

    2. 打开记事本文件
    powershell
    $document = $notepad.Documents.Open("C:\\path\\to\\file.txt")

    3. 读取记事本文件内容
    powershell
    $content = $document.Content.Text

    4. 写入记事本文件内容
    powershell
    $document.Content.Text = "Hello, world!"

    5. 保存记事本文件
    powershell
    $document.Save()

    6. 设置打印页面边距
    powershell
    $document.PageSetup.LeftMargin = 36
    $document.PageSetup.RightMargin = 36
    $document.PageSetup.TopMargin = 36
    $document.PageSetup.BottomMargin = 36

    7. 设置页眉页码格式
    powershell
    $section = $document.Sections.Item(1)
    $header = $section.Headers.Item(1)
    $header.PageNumbers.Add(1)
    $header.Range.Text = "Page "

    完整代码如下:
    powershell
    $notepad = New-Object -ComObject "Word.Application"
    $document = $notepad.Documents.Open("C:\\path\\to\\file.txt")
    $content = $document.Content.Text
    $document.Content.Text = "Hello, world!"
    $document.Save()
    $document.PageSetup.LeftMargin = 36
    $document.PageSetup.RightMargin = 36
    $document.PageSetup.TopMargin = 36
    $document.PageSetup.BottomMargin = 36
    $section = $document.Sections.Item(1)
    $header = $section.Headers.Item(1)
    $header.PageNumbers.Add(1)
    $header.Range.Text = "Page "
    $notepad.Quit()

    注意:在操作完成后,需要关闭记事本应用程序对象,否则会一直占用系统资源。
    参考文献:https://docs.microsoft.com/en-us/office/vba/api/overview/word/object-model
    评论

报告相同问题?

问题事件

  • 系统已结题 4月6日
  • 创建了问题 3月29日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料