如何将每张 google 幻灯片转换为 PDF 和 JPG?
How can I convert each google slide into a PDF and JPG?
Google Apps 脚本
- 我想为 Google 幻灯片文件的每张幻灯片创建 PDF 文件 (带有文字和图表的普通幻灯片)。
- 我回顾了过去的问题,找到了一个生成 PDF 文件但将幻灯片作为图像的解决方案 (我需要能够在这些 pdf 中搜索单词)。
- 已应用另一个解决方案来转换电子表格 -> PDF,但它对我的情况不起作用。
This was my reference
function autoFlyerGenerator() {
const name_file = "FILE_NAME";
// Template file in Google slide
const ppt_file = DriveApp.getFileById(##SLIDE_ID##);
// Temporal folder to store a temporal slide file
const temp_folder = DriveApp.getFolderById(##TEMP_FOLDER_ID##);
// PDF folder
const pdf_folder = DriveApp.getFolderById(##PDF_FOLDER_ID##);
// My current spreadsheet
const ss = SpreadsheetApp.getActive();
const values_ss = ss.getDataRange().getValues();
const temp_ppt = ppt_file.makeCopy(temp_folder).setName(name_file);
const open_temp_ppt = SlidesApp.openById(temp_ppt.getId());
const slides = open_temp_ppt.getSlides();
// First for-loop iterate over all rows (without the header)
// Second for-loop iterate over all columns
for (var index = 0; index < ss.getLastRow()-1; index++){
for (var col = 0; col < ss.getLastColumn(); col++){
slides[index].replaceAllText(values_ss[0][col], values_ss[index+1][col])
}
}
open_temp_ppt.saveAndClose();
// This code block create a single PDF of the slide
const pdfContentBlob = temp_ppt.getBlob();
pdf_folder.createFile(pdfContentBlob).setName(name_file);
}
更新
- 我忘了说 Google 幻灯片文件的页面设置大小与默认大小不同。
- 这个问题的第一个答案解决了生成pdf文件的问题,但是没有考虑幻灯片的大小。
我相信你的目标如下。
- 您想将 Google 幻灯片的每一页导出为每个 PDF 文件。
- 您想使用 Google Apps 脚本实现此目的。
这样的话,下面的流程如何?
- 从源 Google 幻灯片中检索所有幻灯片。
- 创建临时 Google 幻灯片。
- 将每一页导出为 PDF 文件。
- 删除临时 Google 幻灯片。
当这个流程反映到一个脚本中,就变成了下面这样。
示例脚本:
请将以下脚本复制并粘贴到 Google 幻灯片和 运行 函数 myFunction
的脚本编辑器中。这样,脚本就是运行.
function myFunction() {
const folderId = "###"; // Please set the folder ID you want to put the exported PDF files.
// 1. Retrieve all slides from the source Google Slides.
const slide = SlidesApp.getActivePresentation();
const srcSlides = slide.getSlides();
// 2. Create a temporal Google Slides.
let temp = SlidesApp.create("temp");
const id = temp.getId();
const file = DriveApp.getFileById(id);
const folder = DriveApp.getFolderById(folderId);
// 3. Export each page as a PDF file.
srcSlides.forEach((s, i) => {
temp.appendSlide(s);
temp.getSlides()[0].remove();
temp.saveAndClose();
folder.createFile(file.getBlob().setName(`page_${i + 1}.pdf`));
temp = SlidesApp.openById(id);
});
// 4. Remove the temporal Google Slides.
file.setTrashed(true);
}
- 在这种情况下,每个 PDF 文件的文件名类似于
page_1.pdf
、page_2.pdf
。
注:
从你的标题来看,如果你想将每张幻灯片导出为每个 JPEG 文件而不是 PDF 文件,你也可以使用以下示例脚本。在这种情况下,使用幻灯片 API。所以,在 运行 脚本之前,please enable Slides API at Advanced Google services.
function myFunction() {
const folderId = "###"; // Please set the folder ID you want to put the exported PDF files.
// 1. Retrieve all slides from the source Google Slides.
const slide = SlidesApp.getActivePresentation();
const id = slide.getId();
const srcSlides = slide.getSlides();
// 2. Export each page as a JPEG file.
const folder = DriveApp.getFolderById(folderId);
srcSlides.forEach((s, i) => {
const url = Slides.Presentations.Pages.getThumbnail(id, s.getObjectId(), {"thumbnailProperties.mimeType": "PNG"}).contentUrl;
const blob = UrlFetchApp.fetch(url).getAs(MimeType.JPEG);
folder.createFile(blob.setName(`page_${i + 1}.jpg`));
});
}
- 在这种情况下,每个 JPEG 文件的文件名类似于
page_1.jpg
、page_2.jpg
。
参考文献:
已添加:
来自以下OP的回复,
My Slide file has a different page setup than the default, so when applying your code the PDF files generated end up with the default size, giving distorted files. How can I address that?
Actually, it's my bad. I mean that my original Google Slide file has a different page setup (let's say 14 x 18 cm). Your code works, but the PDF files generated have the default page setup of a slide.
从上面的回复来看,页面大小好像不是默认的。在这种情况下,我想建议使用原始 Google Slides 的页面大小。示例脚本如下
示例脚本:
function myFunction2() {
const folderId = "###"; // Please set the folder ID you want to put the exported PDF files.
// 1. Retrieve all slides from the source Google Slides.
const slide = SlidesApp.getActivePresentation();
const srcId = slide.getId();
const srcSlides = slide.getSlides();
// 2. Create a temporal Google Slides.
const file = DriveApp.getFileById(srcId).makeCopy("temp");
const id = file.getId();
let temp = SlidesApp.openById(id);
temp.getSlides().forEach((e, i) => {
if (i != 0) e.remove();
});
const folder = DriveApp.getFolderById(folderId);
// 3. Export each page as a PDF file.
srcSlides.forEach((s, i) => {
temp.appendSlide(s);
temp.getSlides()[0].remove();
temp.saveAndClose();
folder.createFile(file.getBlob().setName(`page_${i + 1}.pdf`));
temp = SlidesApp.openById(id);
});
// 4. Remove the temporal Google Slides.
file.setTrashed(true);
}
Google Apps 脚本
- 我想为 Google 幻灯片文件的每张幻灯片创建 PDF 文件 (带有文字和图表的普通幻灯片)。
- 我回顾了过去的问题,找到了一个生成 PDF 文件但将幻灯片作为图像的解决方案 (我需要能够在这些 pdf 中搜索单词)。
- 已应用另一个解决方案来转换电子表格 -> PDF,但它对我的情况不起作用。
This was my reference
function autoFlyerGenerator() {
const name_file = "FILE_NAME";
// Template file in Google slide
const ppt_file = DriveApp.getFileById(##SLIDE_ID##);
// Temporal folder to store a temporal slide file
const temp_folder = DriveApp.getFolderById(##TEMP_FOLDER_ID##);
// PDF folder
const pdf_folder = DriveApp.getFolderById(##PDF_FOLDER_ID##);
// My current spreadsheet
const ss = SpreadsheetApp.getActive();
const values_ss = ss.getDataRange().getValues();
const temp_ppt = ppt_file.makeCopy(temp_folder).setName(name_file);
const open_temp_ppt = SlidesApp.openById(temp_ppt.getId());
const slides = open_temp_ppt.getSlides();
// First for-loop iterate over all rows (without the header)
// Second for-loop iterate over all columns
for (var index = 0; index < ss.getLastRow()-1; index++){
for (var col = 0; col < ss.getLastColumn(); col++){
slides[index].replaceAllText(values_ss[0][col], values_ss[index+1][col])
}
}
open_temp_ppt.saveAndClose();
// This code block create a single PDF of the slide
const pdfContentBlob = temp_ppt.getBlob();
pdf_folder.createFile(pdfContentBlob).setName(name_file);
}
更新
- 我忘了说 Google 幻灯片文件的页面设置大小与默认大小不同。
- 这个问题的第一个答案解决了生成pdf文件的问题,但是没有考虑幻灯片的大小。
我相信你的目标如下。
- 您想将 Google 幻灯片的每一页导出为每个 PDF 文件。
- 您想使用 Google Apps 脚本实现此目的。
这样的话,下面的流程如何?
- 从源 Google 幻灯片中检索所有幻灯片。
- 创建临时 Google 幻灯片。
- 将每一页导出为 PDF 文件。
- 删除临时 Google 幻灯片。
当这个流程反映到一个脚本中,就变成了下面这样。
示例脚本:
请将以下脚本复制并粘贴到 Google 幻灯片和 运行 函数 myFunction
的脚本编辑器中。这样,脚本就是运行.
function myFunction() {
const folderId = "###"; // Please set the folder ID you want to put the exported PDF files.
// 1. Retrieve all slides from the source Google Slides.
const slide = SlidesApp.getActivePresentation();
const srcSlides = slide.getSlides();
// 2. Create a temporal Google Slides.
let temp = SlidesApp.create("temp");
const id = temp.getId();
const file = DriveApp.getFileById(id);
const folder = DriveApp.getFolderById(folderId);
// 3. Export each page as a PDF file.
srcSlides.forEach((s, i) => {
temp.appendSlide(s);
temp.getSlides()[0].remove();
temp.saveAndClose();
folder.createFile(file.getBlob().setName(`page_${i + 1}.pdf`));
temp = SlidesApp.openById(id);
});
// 4. Remove the temporal Google Slides.
file.setTrashed(true);
}
- 在这种情况下,每个 PDF 文件的文件名类似于
page_1.pdf
、page_2.pdf
。
注:
从你的标题来看,如果你想将每张幻灯片导出为每个 JPEG 文件而不是 PDF 文件,你也可以使用以下示例脚本。在这种情况下,使用幻灯片 API。所以,在 运行 脚本之前,please enable Slides API at Advanced Google services.
function myFunction() { const folderId = "###"; // Please set the folder ID you want to put the exported PDF files. // 1. Retrieve all slides from the source Google Slides. const slide = SlidesApp.getActivePresentation(); const id = slide.getId(); const srcSlides = slide.getSlides(); // 2. Export each page as a JPEG file. const folder = DriveApp.getFolderById(folderId); srcSlides.forEach((s, i) => { const url = Slides.Presentations.Pages.getThumbnail(id, s.getObjectId(), {"thumbnailProperties.mimeType": "PNG"}).contentUrl; const blob = UrlFetchApp.fetch(url).getAs(MimeType.JPEG); folder.createFile(blob.setName(`page_${i + 1}.jpg`)); }); }
- 在这种情况下,每个 JPEG 文件的文件名类似于
page_1.jpg
、page_2.jpg
。
- 在这种情况下,每个 JPEG 文件的文件名类似于
参考文献:
已添加:
来自以下OP的回复,
My Slide file has a different page setup than the default, so when applying your code the PDF files generated end up with the default size, giving distorted files. How can I address that?
Actually, it's my bad. I mean that my original Google Slide file has a different page setup (let's say 14 x 18 cm). Your code works, but the PDF files generated have the default page setup of a slide.
从上面的回复来看,页面大小好像不是默认的。在这种情况下,我想建议使用原始 Google Slides 的页面大小。示例脚本如下
示例脚本:
function myFunction2() {
const folderId = "###"; // Please set the folder ID you want to put the exported PDF files.
// 1. Retrieve all slides from the source Google Slides.
const slide = SlidesApp.getActivePresentation();
const srcId = slide.getId();
const srcSlides = slide.getSlides();
// 2. Create a temporal Google Slides.
const file = DriveApp.getFileById(srcId).makeCopy("temp");
const id = file.getId();
let temp = SlidesApp.openById(id);
temp.getSlides().forEach((e, i) => {
if (i != 0) e.remove();
});
const folder = DriveApp.getFolderById(folderId);
// 3. Export each page as a PDF file.
srcSlides.forEach((s, i) => {
temp.appendSlide(s);
temp.getSlides()[0].remove();
temp.saveAndClose();
folder.createFile(file.getBlob().setName(`page_${i + 1}.pdf`));
temp = SlidesApp.openById(id);
});
// 4. Remove the temporal Google Slides.
file.setTrashed(true);
}