Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
---
category: general
date: 2026-07-18
description: حوّل HTML إلى EPUB في بايثون بسرعة. تعلّم كيفية تحميل ملف HTML في بايثون
وأيضًا تحويل HTML إلى MHTML باستخدام مكتبات بسيطة.
draft: false
images:
- PLACEHOLDER_URL/og-image.png
keywords:
- convert html to epub
- load html file in python
- convert html to mhtml
language: ar
lastmod: 2026-07-18
og_description: تحويل HTML إلى EPUB باستخدام بايثون مع مثال واضح وقابل للتنفيذ. كما
يمكنك تعلم كيفية تحميل ملف HTML في بايثون وتحويل HTML إلى MHTML في دقائق.
og_image_alt: Diagram showing convert html to epub workflow
og_title: تحويل HTML إلى EPUB في بايثون – دليل كامل
schemas:
- author: Aspose
dateModified: '2026-07-18'
description: Convert HTML to EPUB in Python quickly. Learn how to load HTML file
in Python and also convert HTML to MHTML using simple libraries.
headline: Convert HTML to EPUB in Python – Complete Step‑by‑Step Guide
type: TechArticle
- description: Convert HTML to EPUB in Python quickly. Learn how to load HTML file
in Python and also convert HTML to MHTML using simple libraries.
name: Convert HTML to EPUB in Python – Complete Step‑by‑Step Guide
steps:
- name: '**Python 3.9+** – the syntax used here works on any recent version.'
text: '**Python 3.9+** – the syntax used here works on any recent version.'
- name: '**pip** – to install third‑party packages.'
text: '**pip** – to install third‑party packages.'
- name: A simple HTML file, e.g., `sample.html`, placed in a folder you control.
text: A simple HTML file, e.g., `sample.html`, placed in a folder you control.
type: HowTo
tags:
- python
- html
- epub
- mhtml
- file conversion
title: تحويل HTML إلى EPUB باستخدام بايثون – دليل شامل خطوة بخطوة
url: /ar/python/general/convert-html-to-epub-in-python-complete-step-by-step-guide/
---

{{< blocks/products/pf/main-wrap-class >}}
{{< blocks/products/pf/main-container >}}
{{< blocks/products/pf/tutorial-page-section >}}

# تحويل HTML إلى EPUB في بايثون – دليل كامل خطوة بخطوة

هل تساءلت يومًا كيف **تحويل HTML إلى EPUB** دون أن تشد شعرك؟ لست وحدك—المطورون بحاجة مستمرة إلى تحويل صفحات الويب إلى كتب إلكترونية للقراءة دون اتصال، والقيام بذلك في بايثون سهل بشكل مفاجئ. في هذا الدرس سنستعرض تحميل ملف HTML في بايثون، تحويل هذا الـHTML إلى EPUB، وحتى تحويل نفس المصدر إلى MHTML لأرشفة صديقة للبريد الإلكتروني.

بنهاية الدليل ستحصل على سكريبت جاهز للتنفيذ يأخذ ملف `sample.html` واحدًا ويولد كلًا من `sample.epub` و `sample.mhtml`. لا أسرار، فقط كود واضح وتفسيرات.

---

![Conversion pipeline diagram](https://example.com/images/convert-html-epub.png "Diagram showing convert html to epub workflow")

## ما ستبنيه

- **تحميل ملف HTML في بايثون** باستخدام الوحدات المدمجة `pathlib` و `io`.
- **تحويل HTML إلى EPUB** باستخدام مكتبة `ebooklib` التي تتعامل مع تنسيق حاوية EPUB نيابةً عنك.
- **تحويل HTML إلى MHTML** (المعرف أيضًا باسم MHT) باستخدام حزمة `email`، لإنشاء أرشيف ويب بملف واحد يمكن للمتصفحات فتحه مباشرة.

سنغطي أيضًا:

- التبعيات المطلوبة وكيفية تثبيتها.
- معالجة الأخطاء بحيث يفشل السكريبت بأناقة.
- كيفية تخصيص البيانات الوصفية مثل العنوان والمؤلف لملف EPUB.

جاهز؟ هيا نبدأ.

---

## المتطلبات المسبقة

قبل أن نبدأ بالبرمجة، تأكد من أن لديك:

1. **Python 3.9+** – الصياغة المستخدمة هنا تعمل على أي نسخة حديثة.
2. **pip** – لتثبيت الحزم الخارجية.
3. ملف HTML بسيط، مثل `sample.html`، موجود في مجلد تتحكم فيه.

إذا كان لديك هذه بالفعل، ممتاز—تخطى إلى القسم التالي.

> **نصيحة احترافية:** احرص على أن يكون HTML الخاص بك مُشكلًا جيدًا (أقسام `<head>` و `<body>` صحيحة). بينما ستحاول `ebooklib` إصلاح المشكلات البسيطة، فإن مصدرًا نظيفًا يوفر عليك المتاعب لاحقًا.

---

## الخطوة 1 – تثبيت المكتبات المطلوبة

نحتاج إلى حزمتين خارجيتين:

- **ebooklib** – لتوليد EPUB.
- **beautifulsoup4** – اختياري، لكنه مفيد لتنظيف HTML قبل التحويل.

Run this in your terminal:

```bash
pip install ebooklib beautifulsoup4
```

> **لماذا هذه المكتبات؟** `ebooklib` يبني بنية EPUB القائمة على ZIP لك، ويتعامل تلقائيًا مع مجلد `META‑INF`، وملف `content.opf`، وملف `toc.ncx`. `beautifulsoup4` يتيح لنا ترتيب HTML، مما يضمن أن الكتاب الإلكتروني النهائي يُعرض بشكل صحيح على جميع القارئات.

---

## الخطوة 2 – تحميل ملف HTML في بايثون

تحميل ملف HTML سهل، لكننا سنغلفه في دالة مساعدة صغيرة تُعيد كائن **BeautifulSoup** للمعالجة لاحقًا.

```python
from pathlib import Path
from bs4 import BeautifulSoup

def load_html(filepath: str) -> BeautifulSoup:
"""
Load an HTML file from disk and return a BeautifulSoup object.
Raises FileNotFoundError if the file does not exist.
"""
path = Path(filepath)
if not path.is_file():
raise FileNotFoundError(f"HTML file not found: {filepath}")

# Read the file using UTF‑8 encoding (most common for web content)
html_content = path.read_text(encoding="utf-8")
# Parse with BeautifulSoup for optional cleaning later
soup = BeautifulSoup(html_content, "html.parser")
return soup
```

**Explanation:**
- `Path` يوفّر لنا معالجة ملفات مستقلة عن نظام التشغيل.
- استخدام `read_text` يتجنب كتابة كود `open`/`close` يدوي.
- إرجاع كائن `BeautifulSoup` يعني أنه بإمكاننا لاحقًا إزالة السكريبتات، إصلاح العلامات المكسورة، أو إدخال ورقة أنماط قبل أن **نحول HTML إلى EPUB**.

---

## الخطوة 3 – تحويل HTML إلى EPUB

الآن بعد أن استطعنا **تحميل ملف HTML في بايثون**، دعنا نحوله إلى EPUB نظيف. الدالة أدناه تقبل كائن `BeautifulSoup`، مسار الوجهة، وبيانات وصفية اختيارية.

```python
import uuid
from ebooklib import epub

def html_to_epub(soup: BeautifulSoup,
output_path: str,
title: str = "Untitled Book",
author: str = "Anonymous") -> None:
"""
Convert a BeautifulSoup HTML document to an EPUB file.
"""
# Create a new EPUB book instance
book = epub.EpubBook()

# Set basic metadata – this is what shows up in e‑reader libraries
book.set_identifier(str(uuid.uuid4()))
book.set_title(title)
book.set_language('en')
book.add_author(author)

# Convert the soup back to a string; we could also clean it here
html_str = str(soup)

# Create a chapter (EPUB requires at least one)
chapter = epub.EpubHtml(title=title,
file_name='chap_01.xhtml',
lang='en')
chapter.content = html_str
book.add_item(chapter)

# Define the spine (reading order) and table of contents
book.toc = (epub.Link('chap_01.xhtml', title, 'intro'),)
book.spine = ['nav', chapter]

# Add default navigation files
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

# Write the final EPUB file
epub.write_epub(output_path, book, {})

print(f"✅ EPUB created at: {output_path}")
```

**Why this works:**
- `ebooklib.epub.EpubBook` ي抽象 حاوية ZIP والملفات المطلوبة في البيان.
- نولد UUID كمعرّف لتجنب تكرار المعرفات إذا أنشأت العديد من الكتب.
- `spine` يحدد للقراء ترتيب الفصول؛ كتاب بفصل واحد يكفي لمعظم مصادر HTML البسيطة.

**Running the conversion:**

```python
if __name__ == "__main__":
html_doc = load_html("YOUR_DIRECTORY/sample.html")
html_to_epub(html_doc,
"YOUR_DIRECTORY/sample.epub",
title="My Sample eBook",
author="Jane Developer")
```

عند تشغيل السكريبت، سترى علامة تحقق خضراء تؤكد موقع ملف EPUB.

---

## الخطوة 4 – تحويل HTML إلى MHTML

MHTML (أو MHT) يجمع HTML وجميع موارده (الصور، CSS) في ملف MIME واحد. حزمة `email` في بايثون يمكنها بناء هذا التنسيق دون تبعيات خارجية.

```python
import mimetypes
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def html_to_mhtml(soup: BeautifulSoup,
output_path: str,
base_url: str = "") -> None:
"""
Convert a BeautifulSoup HTML document to an MHTML file.
`base_url` is used to resolve relative resource links.
"""
# Create the multipart/related container required for MHTML
mhtml = MIMEMultipart('related')
mhtml['Subject'] = 'Converted MHTML document'
mhtml['Content-Type'] = 'multipart/related; type="text/html"'

# Main HTML part
html_part = MIMEText(str(soup), 'html', 'utf-8')
html_part.add_header('Content-Location', 'file://index.html')
mhtml.attach(html_part)

# Optional: embed images referenced in the HTML
for img in soup.find_all('img'):
src = img.get('src')
if not src:
continue

# Resolve absolute path if needed
img_path = Path(base_url) / src if base_url else Path(src)
if not img_path.is_file():
continue # skip missing files

# Guess MIME type; default to octet-stream
mime_type, _ = mimetypes.guess_type(img_path)
mime_type = mime_type or 'application/octet-stream'

with img_path.open('rb') as f:
img_data = f.read()

img_part = MIMEBase(*mime_type.split('/'))
img_part.set_payload(img_data)
encoders.encode_base64(img_part)
img_part.add_header('Content-Location', f'file://{src}')
img_part.add_header('Content-Transfer-Encoding', 'base64')
mhtml.attach(img_part)

# Write the MHTML file
with open(output_path, 'wb') as out_file:
out_file.write(mhtml.as_bytes())

print(f"✅ MHTML created at: {output_path}")
```

**Key points:**

- نوع MIME `multipart/related` يخبر المتصفحات أن جزء HTML وموارده تنتمي معًا.
- نقوم بالتكرار عبر وسوم `<img>` لتضمين الصور؛ إذا لم تحتاج الصور، يمكنك تخطي هذا الجزء.
- `Content-Location` يستخدم URI من نوع `file://` حتى يتمكن المتصفح من حل الموارد داخليًا.

**Calling the function:**

```python
if __name__ == "__main__":
# Re‑use the previously loaded soup
html_doc = load_html("YOUR_DIRECTORY/sample.html")
html_to_mhtml(html_doc, "YOUR_DIRECTORY/sample.mhtml", base_url="YOUR_DIRECTORY")
```

الآن لديك كل من `sample.epub` و `sample.mhtml` جنبًا إلى جنب.

---

## السكريبت الكامل – جميع الخطوات في ملف واحد

فيما يلي السكريبت الكامل الجاهز للتنفيذ الذي يجمع كل شيء. احفظه باسم `convert_html.py` واستبدل `YOUR_DIRECTORY` بالمسار الذي يحتوي على `sample.html` الخاص بك.



## ما الذي يجب أن تتعلمه بعد ذلك؟

الدروس التالية تغطي مواضيع ذات صلة وثيقة تبني على التقنيات الموضحة في هذا الدليل. كل مورد يتضمن أمثلة كود كاملة مع شروحات خطوة بخطوة لمساعدتك على إتقان ميزات API إضافية واستكشاف أساليب تنفيذ بديلة في مشاريعك.

- [How to Convert HTML to MHTML with Aspose.HTML for Java](/html/english/java/conversion-html-to-other-formats/convert-html-to-mhtml/)
- [How to Convert EPUB to PDF with Java – Using Aspose.HTML](/html/english/java/converting-epub-to-pdf/convert-epub-to-pdf/)
- [How to Convert EPUB to Images with Aspose.HTML for Java](/html/english/java/conversion-epub-to-image-and-pdf/convert-epub-to-image/)

{{< /blocks/products/pf/tutorial-page-section >}}
{{< /blocks/products/pf/main-container >}}
{{< /blocks/products/pf/main-wrap-class >}}
{{< blocks/products/products-backtop-button >}}
Loading