Open Source Python API for Google Sheets

مكتبة Python التي تسهل استدعاء Python من Excel والعكس صحيح.

Xlwings هي واجهة برمجة تطبيقات Python مفتوحة المصدر لمعالجة تنسيق ملف Excel. باستخدام واجهة برمجة التطبيقات ، يمكنك أتمتة excel من python لإنتاج التقارير وكتابة UDFs (وظائف محددة بواسطة المستخدم) وكتابة وحدات الماكرو والتحكم في Excel عن بُعد. علاوة على ذلك ، تسمح API بمعالجة هياكل البيانات مثل الخلايا والقوائم والنطاقات ومصفوفات NumPy وإطارات بيانات الباندا وسلسلة الباندا.

يتطلب Xlwings تثبيت Microsoft Excel ، لذلك يعمل كل شيء على Windows و macOS باستثناء أن UDFs يعمل فقط على Windows.

Previous Next

الشروع في العمل مع Xlwings

تحتاج إلى تثبيت Python 3.5 أو أعلى على نظامك لتشغيل xlwings بسلاسة. الطريقة الموصى بها للتثبيت عبر PIP. الرجاء استخدام الأمر التالي.

قم بتثبيت Xlwings عبر أمر PIP

pip install xlwings

قم بتثبيت Xlwings عبر Conda

conda install xlwings

تفاعل مع Excel من Python

تسمح Xlwings API بمعالجة جداول بيانات Microsoft باستخدام Python. باستخدام واجهة برمجة التطبيقات ، يمكنك الاتصال بمصنف موجود أو إنشاء مصنف جديد باستخدام طريقة xlwings.Book (). يمكنك قراءة / كتابة القيم من النطاقات وإليها ، وتوسيع النطاقات ، وتحويل أنواع البيانات بسهولة. علاوة على ذلك ، يمكنك إضافة مخطط Matplotlib & Plotly كصور في مصنفات Excel الخاصة بك.

احصل على أبعاد نطاقات Excel ديناميكيًا عبر Python

sheet = xw.Book().sheets[0]
sheet['A1'].value = [[1,2], [3,4]]
range1 = sheet['A1'].expand('table')  # or just .expand()
range2 = sheet['A1'].options(expand='table')
range1.value
[[1.0, 2.0], [3.0, 4.0]]
range2.value
[[1.0, 2.0], [3.0, 4.0]]
sheet['A3'].value = [5, 6]
range1.value
[[1.0, 2.0], [3.0, 4.0]]
range2.value

اتصل ببايثون من Excel

تتيح مكتبة جداول البيانات مفتوحة المصدر Xlwings لمطوري البرامج التواصل مع Python عبر Excel. يمكنك استدعاء funcitons python داخل ملف Excel الخاص بك إما باستخدام زر Run من الوظيفة الإضافية Xlwings Excel أو باستخدام وظيفة RunPython VBA. الشيء الجيد في استخدام Excel Add-In هو أنك لا تحتاج إلى أن تكون المصنفات الخاصة بك ممكّنة بماكرو ، ويمكنك حفظها كـ xlsx.

استخدم Python Script لإدراج رموز QR في Microsoft Excel

import tempfile
import segno
import xlwings as xw
# Update this with the name of your workbook
book = xw.Book('qr.xlsx')
sheet = xw.sheets[0]
# Update this with the starting cell of your URLs
start_cell = sheet['A1']
urls = start_cell.options(expand='down', ndim=1).value
# Loop through each URL and generate the QR code
for ix, url in enumerate(urls):
    # Generate the QR code
    qr = segno.make(url)
    with tempfile.TemporaryDirectory() as td:
        # Save the QR code as a temporary svg file. If you are on macOS, use pdf
        # instead and if you don't have Microsoft 365, you may have to use png
        filepath = f'{td}/qr.svg'
        qr.save(filepath, scale=5, border=0, finder_dark='#15a43a')
        # Insert the QR code to the right of the URL
        destination_cell = start_cell.offset(row_offset=ix, column_offset=1)
        sheet.pictures.add(filepath,
                           left=destination_cell.left,
                           top=destination_cell.top)

وظائف محددة من قبل المستخدم (UDFs) في Excel باستخدام Python

توفر مكتبة Xlwings للمطورين القدرة على كتابة وظائف محددة بواسطة المستخدم (UDFs) داخل تنسيق ملف Microsoft Excel برمجيًا. حاليًا ، لا تتوفر UDFs إلا على windows. تسمح API باستخدام وظائف بسيطة وعدد أبعاد الصفيف وصيغ الصفيف الديناميكية وصيغ arrya مع NumPy والمزيد.

كيفية استدعاء وحدات الماكرو عبر RunPython في Microsoft Excel

import xlwings as xw
@xw.sub
def my_macro():
    """Writes the name of the Workbook into Range("A1") of Sheet 1"""
    wb = xw.Book.caller()
    wb.sheets[0].range('A1').value = wb.name
 عربي