返回列表 发帖

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

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

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

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

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

本帖最后由 flashercs 于 2022-7-3 15:31 编辑
function Get-ImageScaleSize {
  [CmdletBinding()]
  [OutputType([double])]
  param(
    [Parameter(Mandatory = $true)]
    [ValidateScript( { $_ -gt 0 })]
    [double]$ContainerWidth,
    [Parameter(Mandatory = $true)]
    [ValidateScript( { $_ -gt 0 })]
    [double]$ContainerHeight,
    [Parameter(Mandatory = $true)]
    [ValidateScript( { $_ -gt 0 })]
    [double]$ImageWidth,
    [Parameter(Mandatory = $true)]
    [ValidateScript( { $_ -gt 0 })]
    [double]$ImageHeight
  )
  $rContainer = $ContainerWidth / $ContainerHeight
  $rImage = $ImageWidth / $ImageHeight
  if ($rImage -le $rContainer) {
    $h = $ContainerHeight
    $w = $ImageWidth * $ContainerHeight / $ImageHeight
  } else {
    $w = $ContainerWidth
    $h = $ImageHeight * $ContainerWidth / $ImageWidth
  }
  $w, $h
}
function Edit-WWWDoc {
  param (
    [string]$LiteralPath
  )
  $providerPath = Convert-Path -LiteralPath $LiteralPath
  $doc = $wordApp.Documents.Open($providerPath)
  $pageWidth = $doc.PageSetup.PageWidth
  $pageHeight = $doc.PageSetup.PageHeight
  $leftMargin = $doc.PageSetup.LeftMargin
  $rightMargin = $doc.PageSetup.RightMargin
  $topMargin = $doc.PageSetup.TopMargin
  $bottomMargin = $doc.PageSetup.BottomMargin
  $docWidth = $pageWidth - $leftMargin - $rightMargin
  $docHeight = $pageHeight - $topMargin - $bottomMargin
  # InlineShapes
  foreach ($shapeInline in $doc.InlineShapes) {
    $null = $shapeInline.ConvertToShape()
  }
  foreach ($shape in $doc.Shapes) {
    $shape.WrapFormat.Type = $wdWrapTopBottom
    $shape.WrapFormat.Side = $wdWrapBoth
    $shape.LockAspectRatio = $msoTrue
    $w, $h = Get-ImageScaleSize -ContainerWidth $docWidth -ContainerHeight $docHeight -ImageWidth $shape.Width -ImageHeight $shape.Height
    if ($w -lt $shape.Width) {
      $shape.Width = [float]$w
    }
    # $shape.Width
    # $shape.Height
  }
  # Tables
  foreach ($table in $doc.Tables) {
    $table.PreferredWidthType = $wdPreferredWidthPercent
    $table.PreferredWidth = 100
  }
  # clear all text and paragraph formattings
  $doc.Content.Select()
  $wordApp.Selection.ClearFormatting()
  # clear highlight color
  $wordApp.Selection.Range.HighlightColorIndex = $wdNoHighlight
  # remove blank paragraphs
  for ($i = $doc.Paragraphs.Count; $i -ge 1 ; $i--) {
    $para = $doc.Paragraphs[$i]
    if ([string]::IsNullOrWhiteSpace($para.Range.Text)) {
      $null = $para.Range.Delete()
    }
  }
  # Font
  $paraFont = $doc.Content.Font
  $paraFont.NameAscii = "Consolas"
  $paraFont.NameOther = "Consolas"
  $paraFont.NameFarEast = "微软雅黑"
  $paraFont.Size = 12
  # ParagraphFormat
  $paraFormat = $doc.Content.ParagraphFormat
  $paraFormat.LineSpacingRule = $wdLineSpaceExactly
  $paraFormat.LineSpacing = 12
  $paraFormat.LineUnitAfter = 0
  $paraFormat.LineUnitBefore = 0
  $paraFormat.SpaceAfter = 0
  $paraFormat.SpaceBefore = 0
  
  $doc.Close($true)
}
# const
$wdPreferredWidthAuto = 1
$wdPreferredWidthPercent = 2
$wdPreferredWidthPoints = 3
$wdInlineShapePicture = 3
$wdInlineShapeLinkedPicture = 4
$wdWrapSquare = 0
$wdWrapBoth = 0
$wdWrapTopBottom = 4
$wdLineSpaceExactly = 4
$wdLineSpaceSingle = 0
$wdLineSpaceMultiple = 5
$wdLineSpaceAtLeast = 3
$wdNoHighlight = 0
$msoTrue = -1
$msoLinkedPicture = 11
$msoPicture = 13
# main
$wordApp = New-Object -ComObject Word.Application -ErrorAction Stop
Edit-WWWDoc -LiteralPath .\安.docx
$wordApp.Quit()COPY
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

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

TOP

返回列表