[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] [已解决]powershell修改word中特定满足条件的图片和表格的大小

本帖最后由 5i365 于 2022-6-28 19:34 编辑

情况是这样的, 我从网页上贴到word中的内容:
如果包含有图片, 小图片没事儿, 但图片大的话, 只能在页面上显示一部分, 需要手动调对角线, 来缩放到文档的页面宽度, 图片多的时候, 操作有些麻烦
如果包含有表示, 有的表格宽度也越出页面了, 但不是对角线调了, 只调最右边的框线就行了
另外文字的行距也太大了, 每次都要设成固定行高12磅

逻辑有了:
PS脚本遍历检查word中的
所有图片: 当图片宽度超过页面宽度时, 等比例调整图片的宽度为页面宽度
所有表格: 当表格宽度超过页面宽度时, 调最右边的框线为页面宽度
所有文字: 统一设置固定的行高12磅

搜索百度和论坛无果, 求路过大侠指引, 提前感谢
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

本帖最后由 flashercs 于 2022-7-3 15:31 编辑
  1. function Get-ImageScaleSize {
  2.   [CmdletBinding()]
  3.   [OutputType([double])]
  4.   param(
  5.     [Parameter(Mandatory = $true)]
  6.     [ValidateScript( { $_ -gt 0 })]
  7.     [double]$ContainerWidth,
  8.     [Parameter(Mandatory = $true)]
  9.     [ValidateScript( { $_ -gt 0 })]
  10.     [double]$ContainerHeight,
  11.     [Parameter(Mandatory = $true)]
  12.     [ValidateScript( { $_ -gt 0 })]
  13.     [double]$ImageWidth,
  14.     [Parameter(Mandatory = $true)]
  15.     [ValidateScript( { $_ -gt 0 })]
  16.     [double]$ImageHeight
  17.   )
  18.   $rContainer = $ContainerWidth / $ContainerHeight
  19.   $rImage = $ImageWidth / $ImageHeight
  20.   if ($rImage -le $rContainer) {
  21.     $h = $ContainerHeight
  22.     $w = $ImageWidth * $ContainerHeight / $ImageHeight
  23.   } else {
  24.     $w = $ContainerWidth
  25.     $h = $ImageHeight * $ContainerWidth / $ImageWidth
  26.   }
  27.   $w, $h
  28. }
  29. function Edit-WWWDoc {
  30.   param (
  31.     [string]$LiteralPath
  32.   )
  33.   $providerPath = Convert-Path -LiteralPath $LiteralPath
  34.   $doc = $wordApp.Documents.Open($providerPath)
  35.   $pageWidth = $doc.PageSetup.PageWidth
  36.   $pageHeight = $doc.PageSetup.PageHeight
  37.   $leftMargin = $doc.PageSetup.LeftMargin
  38.   $rightMargin = $doc.PageSetup.RightMargin
  39.   $topMargin = $doc.PageSetup.TopMargin
  40.   $bottomMargin = $doc.PageSetup.BottomMargin
  41.   $docWidth = $pageWidth - $leftMargin - $rightMargin
  42.   $docHeight = $pageHeight - $topMargin - $bottomMargin
  43.   # InlineShapes
  44.   foreach ($shapeInline in $doc.InlineShapes) {
  45.     $null = $shapeInline.ConvertToShape()
  46.   }
  47.   foreach ($shape in $doc.Shapes) {
  48.     $shape.WrapFormat.Type = $wdWrapTopBottom
  49.     $shape.WrapFormat.Side = $wdWrapBoth
  50.     $shape.LockAspectRatio = $msoTrue
  51.     $w, $h = Get-ImageScaleSize -ContainerWidth $docWidth -ContainerHeight $docHeight -ImageWidth $shape.Width -ImageHeight $shape.Height
  52.     if ($w -lt $shape.Width) {
  53.       $shape.Width = [float]$w
  54.     }
  55.     # $shape.Width
  56.     # $shape.Height
  57.   }
  58.   # Tables
  59.   foreach ($table in $doc.Tables) {
  60.     $table.PreferredWidthType = $wdPreferredWidthPercent
  61.     $table.PreferredWidth = 100
  62.   }
  63.   # clear all text and paragraph formattings
  64.   $doc.Content.Select()
  65.   $wordApp.Selection.ClearFormatting()
  66.   # clear highlight color
  67.   $wordApp.Selection.Range.HighlightColorIndex = $wdNoHighlight
  68.   # remove blank paragraphs
  69.   for ($i = $doc.Paragraphs.Count; $i -ge 1 ; $i--) {
  70.     $para = $doc.Paragraphs[$i]
  71.     if ([string]::IsNullOrWhiteSpace($para.Range.Text)) {
  72.       $null = $para.Range.Delete()
  73.     }
  74.   }
  75.   # Font
  76.   $paraFont = $doc.Content.Font
  77.   $paraFont.NameAscii = "Consolas"
  78.   $paraFont.NameOther = "Consolas"
  79.   $paraFont.NameFarEast = "微软雅黑"
  80.   $paraFont.Size = 12
  81.   # ParagraphFormat
  82.   $paraFormat = $doc.Content.ParagraphFormat
  83.   $paraFormat.LineSpacingRule = $wdLineSpaceExactly
  84.   $paraFormat.LineSpacing = 12
  85.   $paraFormat.LineUnitAfter = 0
  86.   $paraFormat.LineUnitBefore = 0
  87.   $paraFormat.SpaceAfter = 0
  88.   $paraFormat.SpaceBefore = 0
  89.   
  90.   $doc.Close($true)
  91. }
  92. # const
  93. $wdPreferredWidthAuto = 1
  94. $wdPreferredWidthPercent = 2
  95. $wdPreferredWidthPoints = 3
  96. $wdInlineShapePicture = 3
  97. $wdInlineShapeLinkedPicture = 4
  98. $wdWrapSquare = 0
  99. $wdWrapBoth = 0
  100. $wdWrapTopBottom = 4
  101. $wdLineSpaceExactly = 4
  102. $wdLineSpaceSingle = 0
  103. $wdLineSpaceMultiple = 5
  104. $wdLineSpaceAtLeast = 3
  105. $wdNoHighlight = 0
  106. $msoTrue = -1
  107. $msoLinkedPicture = 11
  108. $msoPicture = 13
  109. # main
  110. $wordApp = New-Object -ComObject Word.Application -ErrorAction Stop
  111. Edit-WWWDoc -LiteralPath .\安.docx
  112. $wordApp.Quit()
复制代码
1

评分人数

    • 5i365: 乐于分享, 技术牛X技术 + 1
微信:flashercs
QQ:49908356

TOP

本帖最后由 5i365 于 2022-6-28 18:35 编辑

回复 2# flashercs

感谢帮忙, 刚试了一下, 所有的图片, 不管小的大的, 都拉到页宽了, 逻辑应该是, 只有大于页宽的图片才设到页宽
另外, 文字大小没有变化, 我选定文字看了一下, 段落 -> 段后 设为了12磅, 行间距没有变化, 应该设行间距设为固定值12磅
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

TOP

本帖最后由 flashercs 于 2022-6-28 19:20 编辑

回复 3# 5i365


    改了一下;
微信:flashercs
QQ:49908356

TOP

回复 4# flashercs


   大侠牛X, 用的英文版的office, 不过感觉这样挺好, 写一些命令的时候, 看英文方便些
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

TOP

回复 5# 5i365


    我又修改了一下, 修正不同段落前后 间距不一致的问题.
微信:flashercs
QQ:49908356

TOP

回复 6# flashercs

多谢大侠完善, 我刚刚又加了下面几行设置中英文字体的代码, 但是行距不生效了, 始终是单倍行距
  1.   # ParagraphFont
  2.   $paraFont = $doc.Content.font
  3.   $paraFont.NameFarEast = "微软雅黑"
  4.   $paraFont.NameAscii = "Consolas"
  5.   $paraFont.NameOther = "Consolas"
复制代码
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

TOP

返回列表