使用 powershell 从 txt 文件中读取 urls 的列表,并将每个 url 保存为 pdf,并使用 url 最后一部分的名称
Read list of urls from txt file using powershell and save each url to pdf with name from the last part of the url
以下是从 txt 文件中读取 urls 并将每个 url 保存为 pdf 的 powershell 代码。这里每个url都保存为number.pdf。我希望每个 pdf 都以 url.
的最后一部分命名
例如:如果 url 是“https://www.prodevelopertutorial.com/lte-chapter-1-lte-introduction/”,我希望保存的 pdf 文件是“lte-chapter-1-lte-introduction.pdf”
我从网站上获得了代码。任何人都可以根据我的要求修改它。
$sourceFile = "D:\BATCH-PRINT-WEBPAGES-PDF\D\links2.txt" # the source file containing the URLs you want to convert
$destFolder = "D:\BATCH-PRINT-WEBPAGES-PDF\sharednotes\" # converted PDFs will be saved here. Folder has to exist.
$num = 0
foreach($link in [System.IO.File]::ReadLines($sourceFile))
{
$num++
$outfile = $num.ToString() + '.pdf'
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destFolder $outfile" "$link"
Start-Sleep -s 3
}
根据我在互联网上收集到的信息,我做了以下事情:
$sourceFile = "D:\BATCH-PRINT-WEBPAGES-PDF\Version 1\linktst.txt" # the source file containing the URLs you want to convert
$destFolder = "D:\BATCH-PRINT-WEBPAGES-PDF\Version 1\OP\" # converted PDFs will be saved here. Folder has to exist.
$links= Get-Content -Path D:\BATCH-PRINT-WEBPAGES-PDF\Version1\linktst.txt
$num = 0
foreach($l in $links)
{
z=[uri]'l'
$nam = z.segment[-2]
$num++
$outfile = $nam.ToString() + '.pdf'
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destFolder $outfile" "$link"
Start-Sleep -s 3
}
它不工作。
文本文件中的每个条目都是一行。
https://www.prodevelopertutorial.com/lte-chapter-1-lte-introduction/
https://www.prodevelopertutorial.com/lte-network-architecture/
https://www.prodevelopertutorial.com/4g-lte-tutorial-brief-working-of-network-elements-in-lte-architecture/
https://www.prodevelopertutorial.com/introduction-to-e-utran-network-architecture-elements/
https://www.prodevelopertutorial.com/introduction-to-epc-network-architecture-elements/
每个 url 在文本中换行。
在您的代码中,您使用的是 $outfile = $nam.ToString() + '.pdf'
您将 $nam 值声明为 0 并增加了每个循环的数量。使用数字创建文件的位置。
您可以在下面试试。我的机器上没有 chrome.xe,所以没有测试输出文件的创建。
$srcfile = "E:\Workspace\Test\Test.txt"
$destloc = "E:\Workspace\Test\Dest\"
$data = Get-Content $srcfile
foreach($url in $data){
#Write-Output $url
$url_trim = $url.Trim()
if($url_trim.EndsWith("/"))
{
$url_trim = $url_trim.Substring(0,$url_trim.Length -1 )
}
#Write-Host $url_trim -ForegroundColor Cyan
$filename = $url_trim.Substring($url_trim.LastIndexOf("/")+1)
#Write-Output $filename
$outfile = "$filename.pdf"
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destloc $outfile" "$url"
Start-Sleep -s 3
#Write-Output $outfile
}
以下是从 txt 文件中读取 urls 并将每个 url 保存为 pdf 的 powershell 代码。这里每个url都保存为number.pdf。我希望每个 pdf 都以 url.
的最后一部分命名例如:如果 url 是“https://www.prodevelopertutorial.com/lte-chapter-1-lte-introduction/”,我希望保存的 pdf 文件是“lte-chapter-1-lte-introduction.pdf”
我从网站上获得了代码。任何人都可以根据我的要求修改它。
$sourceFile = "D:\BATCH-PRINT-WEBPAGES-PDF\D\links2.txt" # the source file containing the URLs you want to convert
$destFolder = "D:\BATCH-PRINT-WEBPAGES-PDF\sharednotes\" # converted PDFs will be saved here. Folder has to exist.
$num = 0
foreach($link in [System.IO.File]::ReadLines($sourceFile))
{
$num++
$outfile = $num.ToString() + '.pdf'
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destFolder $outfile" "$link"
Start-Sleep -s 3
}
根据我在互联网上收集到的信息,我做了以下事情:
$sourceFile = "D:\BATCH-PRINT-WEBPAGES-PDF\Version 1\linktst.txt" # the source file containing the URLs you want to convert
$destFolder = "D:\BATCH-PRINT-WEBPAGES-PDF\Version 1\OP\" # converted PDFs will be saved here. Folder has to exist.
$links= Get-Content -Path D:\BATCH-PRINT-WEBPAGES-PDF\Version1\linktst.txt
$num = 0
foreach($l in $links)
{
z=[uri]'l'
$nam = z.segment[-2]
$num++
$outfile = $nam.ToString() + '.pdf'
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destFolder $outfile" "$link"
Start-Sleep -s 3
}
它不工作。
文本文件中的每个条目都是一行。
https://www.prodevelopertutorial.com/lte-chapter-1-lte-introduction/ https://www.prodevelopertutorial.com/lte-network-architecture/ https://www.prodevelopertutorial.com/4g-lte-tutorial-brief-working-of-network-elements-in-lte-architecture/ https://www.prodevelopertutorial.com/introduction-to-e-utran-network-architecture-elements/ https://www.prodevelopertutorial.com/introduction-to-epc-network-architecture-elements/
每个 url 在文本中换行。
在您的代码中,您使用的是 $outfile = $nam.ToString() + '.pdf'
您将 $nam 值声明为 0 并增加了每个循环的数量。使用数字创建文件的位置。
您可以在下面试试。我的机器上没有 chrome.xe,所以没有测试输出文件的创建。
$srcfile = "E:\Workspace\Test\Test.txt"
$destloc = "E:\Workspace\Test\Dest\"
$data = Get-Content $srcfile
foreach($url in $data){
#Write-Output $url
$url_trim = $url.Trim()
if($url_trim.EndsWith("/"))
{
$url_trim = $url_trim.Substring(0,$url_trim.Length -1 )
}
#Write-Host $url_trim -ForegroundColor Cyan
$filename = $url_trim.Substring($url_trim.LastIndexOf("/")+1)
#Write-Output $filename
$outfile = "$filename.pdf"
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' --headless --print-to-pdf="$destloc $outfile" "$url"
Start-Sleep -s 3
#Write-Output $outfile
}