iT邦幫忙

0

想設計一個Powershell代碼

  • 分享至 

  • xImage

我想設計一個Powershell的代碼是想可以自動去一個網址然後截圖畫面再開一個Word檔並放進裡面,最終目的是可以去十個網址截圖再貼上一個Word裡面
我有個類似的代碼

Purpose: Automate opening multiple URLs in Chrome, taking screenshots, and saving them in a Word document.

Import required assemblies

Add-Type -AssemblyName Microsoft.Office.Interop.Word
Add-Type -AssemblyName System.Windows.Forms

Function to check if Chrome is installed

function Test-ChromeInstalled {
$chromePath = (Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe' -ErrorAction SilentlyContinue).'(Default)'
return [bool]$chromePath
}

Function to take a screenshot

function Take-Screenshot {
param ($filePath)
$bitmap = New-Object System.Drawing.Bitmap([System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Width, [System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen(0, 0, 0, 0, $bitmap.Size)
$bitmap.Save($filePath, [System.Drawing.Imaging.ImageFormat]::Png)
$graphics.Dispose()
$bitmap.Dispose()
}

Define paths

$desktopPath = [Environment]::GetFolderPath("Desktop")
$wordDocPath = Join-Path $desktopPath "Screenshots_of_Links.docx"

Ensure Chrome is installed

if (-not (Test-ChromeInstalled)) {
Write-Host "Error: Google Chrome is not installed. Please install it and try again."
exit
}

Initialize Word application

try {
$wordApp = New-Object -ComObject Word.Application
$wordApp.Visible = $true
$doc = if (Test-Path $wordDocPath) {
$wordApp.Documents.Open($wordDocPath)
} else {
$wordApp.Documents.Add()
}
} catch {
Write-Host "Error: Could not open Microsoft Word. Ensure it is installed."
exit
}

Prompt user for multiple URLs

$urls = Read-Host "Enter multiple URLs (separated by commas)"
$urlsArray = $urls -split "," | ForEach-Object { $.Trim() }
foreach ($url in $urlsArray) {
if (-not ($url -match "^https?://")) {
Write-Host "Invalid URL: $url. Please include 'http://' or 'https://'."
continue
}
try {
Write-Host "Opening $url in Chrome..."
$chromeProcess = Start-Process "chrome.exe" -ArgumentList "--start-fullscreen $url" -PassThru
Start-Sleep -Seconds 5
$screenshotPath = Join-Path $desktopPath ("Screenshot
" + [DateTime]::Now.ToString("yyyyMMdd_HHmmss") + ".png")
Write-Host "Capturing screenshot..."
Take-Screenshot -filePath $screenshotPath
Write-Host "Adding screenshot to Word document..."
$selection = $wordApp.Selection
$selection.TypeText("$url")
$selection.TypeParagraph()
$selection.InlineShapes.AddPicture($screenshotPath) | Out-Null
$selection.TypeParagraph()
Stop-Process -Id $chromeProcess.Id -Force -ErrorAction SilentlyContinue
} catch {
Write-Host "Error processing URL: $_"
}
}

Save and close Word document

$doc.SaveAs([ref]$wordDocPath)
$doc.Close()
$wordApp.Quit()
Write-Host "Word document saved at $wordDocPath"
這個是需要後面手動再輸入URL,但我想做到在裡面就已經可以預先打好URL再自己瀏覽,這能做到嗎?

ccutmis iT邦高手 2 級 ‧ 2025-04-16 17:29:03 檢舉
問ChatGPT: "請幫我寫一個 Python 代碼,可以自動去一個網址然後截圖,把截圖畫面放進一個新開的Word檔,最終目的是可以去十個網址截圖再貼上一個Word裡面"
它就會教你怎麼做了 xD
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
zero
iT邦好手 1 級 ‧ 2025-04-16 22:16:47

程式碼是問AI來的嗎?

改這兩行原始碼就可以了

$urls = Read-Host "Enter multiple URLs (separated by commas)"
$urlsArray = $urls -split "," | ForEach-Object { $.Trim() }

你的原始碼已經針對URL陣列做迴圈處理了,有幾個網址就會截圖幾張圖片

用兩個網址資料測試

https://ithelp.ithome.com.tw/upload/images/20250416/20022284zgsNX3sJzg.jpg

我要發表回答

立即登入回答