كيفية استخراج الصور من مستندات جوجل وشرائح جوجل

تخيل أنك تعمل على مستند Google طويل، أو عرض تقديمي من Google Slides، وتحتاج إلى استخراج جميع الصور المضمنة من النص وحفظها كملفات فردية.
الحل البسيط لمعالجة هذه المشكلة هو كما يلي: قم بتحويل مستند Google أو Google Slide إلى صفحة ويب. وإليك كيف يمكنك القيام بذلك:
انتقل إلى القائمة “ملف”. حدد القائمة الفرعية “مشاركة” ثم اختر “نشر على الويب”. سيتم إنشاء صفحة ويب عامة تحتوي على جميع الصور من المستند أو الشريحة الخاصة بك. يمكنك ببساطة النقر بزر الماوس الأيمن فوق الصورة الموجودة على الصفحة وتحديد خيار “حفظ الصورة” وتنزيلها على القرص المحلي لديك.
ما ناقشناه للتو هو عملية يدوية ولكن يمكننا بسهولة تشغيلها تلقائيًا بمساعدة Google Apps Script.
افتح مستند Google الذي يحتوي على الصور، وانتقل إلى قائمة الإضافات واختر Apps Script. انسخ وألصق الكود أدناه وقم بتشغيل saveGoogleDocsImages
وظيفة تنزيل جميع الصور إلى مجلد معين في Google Drive.
يتم ترقيم الصور بشكل تسلسلي وامتداد الملف هو نفس امتداد الصورة المضمنة المضمنة.
function saveGoogleDocsImages() {
// Define the folder name where the extracted images will be saved
const folderName = 'Document Images';
// Check if a folder with the specified name already exists
const folders = DriveApp.getFoldersByName(folderName);
// If the folder exists, use it; otherwise, create a new folder
const folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(folderName);
// Get all the images in the document's body and loop through each image
DocumentApp.getActiveDocument()
.getBody()
.getImages()
.forEach((image, index) => {
// Get the image data as a Blob
const blob = image.getBlob();
// Extract the file extension from the Blob's content type (e.g., 'jpeg', 'png')
const [, fileExtension] = blob.getContentType().split("https://www.labnol.org/");
// Generate a unique file name for each image based on its position in the document
const fileName = `Image #${index + 1}.${fileExtension}`;
// Set the Blob's name to the generated file name
blob.setName(fileName);
// Create a new file in the specified folder with the image data
folder.createFile(blob);
// Log a message indicating that the image has been saved
Logger.log(`Saved ${fileName}`);
});
}
يشبه رمز Apps Script لتنزيل الصور من عرض تقديمي لـ Google Slides. تتكرر الوظيفة على الشرائح الموجودة في العرض التقديمي، ثم لكل شريحة، تتكرر الوظيفة على الصور الموجودة في تلك الشريحة.
function extractImagesFromSlides() {
// Define the folder name where the extracted images will be saved
const folderName = 'Presentation Images';
// Check if a folder with the specified name already exists
const folders = DriveApp.getFoldersByName(folderName);
// If the folder exists, use it; otherwise, create a new folder
const folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(folderName);
// Iterate through each slide in the active presentation
SlidesApp.getActivePresentation()
.getSlides()
.forEach((slide, slideNumber) => {
// Retrieve all images on the current slide
slide.getImages().forEach((image, index) => {
// Get the image data as a Blob
const blob = image.getBlob();
// Extract the file extension from the Blob's content type (e.g., 'jpeg', 'png')
const fileExtension = blob.getContentType().split("https://www.labnol.org/")[1];
const fileName = `Slide${slideNumber + 1}_Image${index + 1}.${fileExtension}`;
// Set the Blob's name to the generated file name
blob.setName(fileName);
// Create a new file in the specified folder with the image data
folder.createFile(blob);
Logger.log(`Saved ${fileName}`);
});
});
}
اكتشاف المزيد من مرابع التكنولوجيا
اشترك للحصول على أحدث التدوينات المرسلة إلى بريدك الإلكتروني.