如何将 Word 文档转换为 LaTeX 文档,而最终 LaTeX 文档中的所有图像都应为 DPI 为 300 的 .png 文件?
可以使用Pandoc实现从Word到Latex的转换。可以使用以下命令提取所有图像:
pandoc --extract-media ./myMediaFolder doc.docx -o doc.tex
所有图像到
.png
文件的转换可以使用ImageMagick来实现。以下脚本将 image
文件夹中的所有图像转换为 .png
文件。只需将脚本命名为 convertImages.ps1
之类的名称,并将其放在 image
文件夹所在的同一文件夹中即可。在 Windows 上,右键单击并使用 PowerShell 运行。
$InputFolderPath = 'images'
$OutputFolderPath = 'converted_images'
if(!(Test-Path $OutputFolderPath)){
New-Item -ItemType Directory -Force -Path $OutputFolderPath
}
Get-ChildItem -Path $InputFolderPath -Recurse -File |
ForEach-Object {
if ($_.Extension -ne ".png") {
$pngFilename = Join-Path $OutputFolderPath ($_.BaseName + ".png")
magick convert -density 300 $_.FullName $pngFilename
Write-Output "Converted image $($_.FullName) to png format"
}
}
Write-Output "Image conversion completed."
如果您使用的是 Mac 或 Linux,bash 命令如下所示:
#!/bin/bash
InputFolderPath='images'
OutputFolderPath='converted_images'
# if output folder doesn't exist, create it
mkdir -p $OutputFolderPath
# loop through all files in the input directory
for file in $InputFolderPath/*; do
# only operate on files
if [ -f "$file" ]; then
# file extension
extension="${file##*.}"
# if file is not PNG
if [ "$extension" != "png" ] && [ "$extension" != "PNG" ]; then
# Output file name
base=$(basename -- "$file")
base_name="${base%.*}"
png_filename="$OutputFolderPath/$base_name.png"
# Convert the image to PNG (indicator -density 300)
convert -density 300 "$file" "$png_filename"
# Log message
echo "Converted image $file to png format"
fi
fi
done
# conversion complete
echo "Image conversion completed."