TUTOS.EU

Ajouter les images d'un répertoire à un fichier .docx en powershell

Comment ajouter les images .jpg d'un répertoire à un fichier word en powershell

Placez ce script powershell dans un répertoire contenant des images et un fichier word.
Lancez-le, indiquez le nom du fichier .doc ou .docx en précisant l'extension. Exemple :
test.docx

#https://learn-powershell.net/2014/12/31/beginning-with-powershell-and-word/

#requires -version 2.0

Function Add-OSCPicture
{
<#
 	.SYNOPSIS
        Add-OSCPicture is an advanced function which can be used to insert many pictures into a word document.
    .DESCRIPTION
        Add-OSCPicture is an advanced function which can be used to insert many pictures into a word document.
    .PARAMETER  <Path>
		Specifies the path of slide.
    .EXAMPLE
        C:\PS> Add-OSCPicture -WordDocumentPath D:\Word\Document.docx -ImageFolderPath "C:\Users\Public\Pictures\Sample Pictures"
		Action(Insert)                     ImageName
		--------------                     ---------
		Finished                           Chrysanthemum.jpg
		Finished                           Desert.jpg
		Finished                           Hydrangeas.jpg
		Finished                           Jellyfish.jpg
		Finished                           Koala.jpg
		Finished                           Lighthouse.jpg
		Finished                           Penguins.jpg
		Finished                           Tulips.jpg
		
		This command shows how to insert many pictures to word document.
#>
	[CmdletBinding()]
	Param
	(
		[Parameter(Mandatory=$true,Position=0)]
        [Alias('wordpath')]
        [String]$WordDocumentPath,
        [Parameter(Mandatory=$true,Position=1)]
        [Alias('imgpath')]
		[String]$ImageFolderPath
	)

	$wdStory = 6
	$wdMove = 0
	Write-host "ImageFolderPath : $ImageFolderPath"

	If(Test-Path -Path $WordDocumentPath)
	{
		If(Test-Path -Path $ImageFolderPath)
		{
			$WordExtension = (Get-Item -Path $WordDocumentPath).Extension
			If($WordExtension -like ".doc" -or $WordExtension -like ".docx")
			{
				$ImageFiles = Get-ChildItem -Path $ImageFolderPath -Recurse -Include *.emf,*.wmf,*.jpg,*.jpeg,*.jfif,*.png,*.jpe,*.bmp,*.dib,*.rle,*.gif,*.emz,*.wmz,*.pcz,*.tif,*.tiff,*.eps,*.pct,*.pict,*.wpg
				
				If($ImageFiles)
				{
					Write-host "Images trouvées"
					#Create the Word application object
					$WordAPP = New-Object -ComObject Word.Application
					#$WordAPP = $objWord 
					#objDoc = $WordDoc
					$WordDoc = $WordAPP.Documents.Open("$WordDocumentPath")
					$objSelection = $WordAPP.Selection 

					#Foreach($MonObjet in $WordDoc.InlineShapes)
					Foreach($MonObjet in $objSelection.InlineShapes)
					{
                        #Write-host $MonObjet.Title
                        Write-host $MonObjet.Width
                        #$MonObjet.Width = 430
                        #exit

                    }
                    #$WordDoc.Save()
					#$WordDoc.Close()
					#$WordAPP.Quit()#release the object
                    #Exit
				
					Foreach($ImageFile in $ImageFiles)
					{
						$ImageFilePath = $ImageFile.FullName	
						
						$Properties = @{'ImageName' = $ImageFile.Name
										'Action(Insert)' = Try
															{
																#$objSelection.EndKey($wdStory, $wdMove)
																$objSelection.TypeParagraph()
																$MonText = $ImageFile.Name
																$MonText = $MonText.substring(3)
																$MonText = $MonText.substring(0,$($MonText.lastindexofany(".")))
																$MonText = "$MonText`n"
																#$objSelection.TypeText("$($ImageFile.Name)`n")
																$objSelection.TypeText($MonText)
																
																#$currentScriptName = $currentScriptName.substring(0,$($currentScriptName.lastindexofany(".")))
															
																$WordAPP.Selection.EndKey(6)|Out-Null
                                                                $WordApp.Selection.InlineShapes.AddPicture("$ImageFilePath",$False,$True)|Out-Null
																#$WordApp.Selection.InsertNewPage() #insert new page to word
															}
															Catch
															{
																"Unfinished"
															}
										}

					}

					$WordDoc.Save()
					$WordDoc.Close()
					$WordAPP.Quit()#release the object
					[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WordAPP)|Out-Null
					Remove-Variable WordAPP
				}
				Else
				{
					Write-Warning "There is no image in this '$ImageFolderPath' folder."
				}
			}
			Else
			{
				Write-Warning "There is no word document file in this '$WordDocumentPath' folder."
			}
		}
		Else
		{
			Write-Warning "Cannot find path '$ImageFolderPath' because it does not exist."
		}
	}
	Else
	{
		Write-Warning "Cannot find path '$WordDocumentPath' because it does not exist."
	}
	
	
}


$DefaultValue = [string]
$MonResultat = [string]

$DefaultValue = "Test.docx"
$MonResultat = Read-Host "Entrez le nom de votre fichier .doc"

if($MonResultat -eq $null){$MonResultat = $DefaultValue}
if($MonResultat -eq ""){$MonResultat = $DefaultValue}

if ($MonResultat.Length -igt 0){
	$varCheminRepertoireScript = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition)
	#Write-host "varCheminRepertoireScript : $varCheminRepertoireScript"
	Write-host "$varCheminRepertoireScript\$MonResultat"
	If ((Test-Path "$varCheminRepertoireScript\$MonResultat") -eq $True) {
		Add-OSCPicture -WordDocumentPath "$varCheminRepertoireScript\$MonResultat" -ImageFolderPath $varCheminRepertoireScript
	}
	else{
		Write-Host "Le fichier $MonResultat n'existe pas dans $varCheminRepertoireScript"
		Write-Host "Précisez bien l'extention du fichier, comme par exemple .docx"
	}	
	
}
else{
	Write-Host "Vous n'avez rien saisi"
}
Lien vers le fichier : cliquez ici Copier le code

Pages Web

Site WebDescription
Gallery.technet.microsoft.comAppend Text to a Microsoft Word Document

Article(s) en relation(s)

2