إدارة الصور

يسمح Aspose.Cells for Python via .NET للمطورين بإضافة صور إلى جداول البيانات أثناء التشغيل. علاوة على ذلك، يمكن التحكم في موضع هذه الصور أثناء التشغيل، وهو ما سيتم مناقشته بمزيد من التفصيل في الأقسام القادمة.

يشرح هذا المقال كيفية إضافة الصور، وكيفية إدراج صورة تعرض محتوى خلايا معينة.

إضافة الصور

إضافة الصور إلى جدول بيانات سهل للغاية. يستغرق الأمر سوى بضعة أسطر من الكود: اتصل ببساطة بالطريقة add لمجموعة pictures، المغلفة في كائن Worksheet. تأخذ الطريقة add المعاملات التالية:

  • فهرس الصف الأعلى الأيسر، فهرس الصف الأعلى.
  • فهرس العمود الأعلى الأيسر، فهرس العمود الأعلى.
  • اسم ملف الصورة، اسم ملف الصورة، مع المسار الكامل.
from aspose.cells import Workbook
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding a picture at the location of a cell whose row and column indices
# Are 5 in the worksheet. It is "F6" cell
worksheet.pictures.add(5, 5, dataDir + "logo.jpg")
# Saving the Excel file
workbook.save(dataDir + "output.xls")

تحديد مواقع الصور

هناك طريقتان محتملتان للتحكم في موضع الصور باستخدام Aspose.Cells for Python via .NET:

  • تحديد موقع نسبي: تعريف موقع نسبي لارتفاع الصف والعرض.
  • تحديد موقع مطلق: تعريف الموقع الدقيق على الصفحة حيث سيتم إدراج الصورة، على سبيل المثال، 40 بكسل إلى اليسار و 20 بكسل أسفل حافة الخلية.

التحديد النسبي

يمكن للمطورين تحديد مواقع الصور بنسبة لارتفاع الصف وعرض العمود باستخدام الخصائص upper_delta_x و upper_delta_y لكائن Aspose.Cells.Drawing.Picture. يمكن الحصول على كائن Picture من مجموعة pictures عن طريق تمرير فهرس الصورة الخاصة به. يضع هذا المثال صورة في الخلية F6.

from aspose.cells import Workbook
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding a picture at the location of a cell whose row and column indices
# Are 5 in the worksheet. It is "F6" cell
pictureIndex = worksheet.pictures.add(5, 5, dataDir + "logo.jpg")
# Accessing the newly added picture
picture = worksheet.pictures[pictureIndex]
# Positioning the picture proportional to row height and colum width
picture.upper_delta_x = 200
picture.upper_delta_y = 200
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

التحديد المطلق

يمكن للمطورين أيضًا تحديد مواقع الصور بشكل مطلق باستخدام الخصائص left و top لكائن Picture. يضع هذا المثال صورة في الخلية F6، على بعد 60 بكسل من اليسار و10 بكسل من أعلى الخلية.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding a picture at the location of a cell whose row and column indices
# Are 5 in the worksheet. It is "F6" cell
pictureIndex = worksheet.pictures.add(5, 5, dataDir + "logo.jpg")
# Accessing the newly added picture
picture = worksheet.pictures[pictureIndex]
# Absolute positioning of the picture in unit of pixels
picture.left = 60
picture.top = 10
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

إدراج صورة بناءً على مرجع الخلية

يتيح لك Aspose.Cells for Python via .NET عرض محتوى خلية ورقة العمل كشكل صورة. يمكنك ربط الصورة بالخلية التي تحتوي على البيانات التي تريد عرضها. نظرًا لربط الخلية أو النطاق بالعنصر الرسومي، فإن التغييرات التي تجريها على البيانات في تلك الخلية أو النطاق تظهر تلقائيًا في العنصر الرسومي.

إضافة صورة إلى ورقة العمل عن طريق استدعاء الطريقة add_picture من مجموعة ShapeCollection (التي تم تغليفها في كائن Worksheet). حدد نطاق الخلية باستخدام السمة formula لكائن Picture.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate a new Workbook
workbook = Workbook()
# Get the first worksheet's cells collection
cells = workbook.worksheets[0].cells
# Add string values to the cells
cells.get("A1").put_value("A1")
cells.get("C10").put_value("C10")
# Add a blank picture to the D1 cell
pic = workbook.worksheets[0].shapes.add_picture(0, 3, 10, 6, None)
# Specify the formula that refers to the source range of cells
pic.formula = "A1:C10"
# Update the shapes selected value in the worksheet
workbook.worksheets[0].shapes.update_selected_value()
# Save the Excel file.
workbook.save(dataDir + "output.out.xls")

مواضيع متقدمة