=========================
📄 OFFLINE DOCX + EXCEL CHEAT SHEET
=========================
🛠 LIBRARIES:
-------------
1. python-docx → Create and modify Word files
2. pandas → Load, filter, and manipulate Excel data
3. openpyxl → Used by pandas to read/write Excel files
=========================
📦 INSTALLATION
=========================
pip install python-docx pandas openpyxl
=========================
📚 WORD DOCX (python-docx)
=========================
from docx import Document
📌 Load a Word file:
doc = Document("template.docx")
📌 Save a Word file:
doc.save("output.docx")
📌 Access paragraphs:
for para in doc.paragraphs:
print(para.text)
📌 Replace text:
para.text = para.text.replace("old", "new")
📌 Add paragraph:
doc.add_paragraph("Hello world")
📌 Add heading:
doc.add_heading("Title", level=1)
📌 Add page break:
doc.add_page_break()
📌 Add a table:
table = doc.add_table(rows=1, cols=3)
table.style = 'Table Grid'
📌 Add row to table:
row_cells = table.add_row().cells
row_cells[0].text = "Name"
row_cells[1].text = "Role"
row_cells[2].text = "Date"
📌 Access table cells:
for row in table.rows:
for cell in row.cells:
print(cell.text)
📌 Set font size or bold:
from docx.shared import Pt
run = doc.paragraphs[0].runs[0]
run.font.size = Pt(12)
run.bold = True
=========================
📚 EXCEL (pandas + openpyxl)
=========================
import pandas as pd
📌 Read Excel:
df = pd.read_excel("data.xlsx")
📌 Save Excel:
df.to_excel("new_file.xlsx", index=False)
📌 Show columns:
print(df.columns)
📌 Filter rows:
df[df["]"فني شبكات" == ]"الوظيفة
📌 Select columns:
df[[" "التاريخ,"]]"االسم
📌 Loop through rows:
for index, row in df.iterrows():
print(row["]"االسم, row[")]"التاريخ
📌 Slice rows:
df.iloc[0:4]
📌 Add new column:
df["new_col"] = "default_value"
📌 Modify values:
df[" = ]"التاريخdf["]"التاريخ.astype(str)
📌 Append new row:
df.loc[len(df)] = ["12-05-2025" ," "مهندسة بيانات,"]"رنا
📌 Sort rows:
df.sort_values(")"التاريخ
📌 Remove rows:
df = df.drop([0, 1]) # by index
📌 Remove duplicates:
df = df.drop_duplicates()
📌 Fill missing values:
df.fillna("N/A")
=========================
🧩 COMPLETE WORD + EXCEL EXAMPLE
=========================
from docx import Document
import pandas as pd
df = pd.read_excel("data.xlsx")
doc = Document("template.docx")
group = df.iloc[0:4]["]"االسم.tolist()
for i in range(4):
for p in doc.paragraphs:
p.text = p.text.replace(f"({االسمi+1})", group[i] if i < len(group) else "")
doc.save("output.docx")
=========================
📄 python-docx (Word) Examples
=========================
✅ Load and Save Document
--------------------------
from docx import Document
doc = Document("template.docx")
doc.save("output.docx")
✍️ Add Paragraphs and Headings
-------------------------------
doc.add_heading("Report Title", level=1)
doc.add_paragraph("This is a simple paragraph.")
🔁 Replace Text in Paragraphs
-------------------------------
for para in doc.paragraphs:
if "(1 ")االسمin para.text:
para.text = para.text.replace("(1")االسم, ")"عمر
📄 Add Page Break
-------------------------------
doc.add_page_break()
📋 Add Table and Fill It
-------------------------------
table = doc.add_table(rows=1, cols=3)
table.style = "Table Grid"
table.cell(0, 0).text = ""االسم
table.cell(0, 1).text = ""الوظيفة
table.cell(0, 2).text = ""التاريخ
data = [("06-05-2025" ," "مهندس نظم,")"عمر,
("07-05-2025" ," "فني شبكات,"])"أحمد
for name, job, date in data:
cells = table.add_row().cells
cells[0].text = name
cells[1].text = job
cells[2].text = str(date)
🎨 Format Text (Font, Size, Bold)
-------------------------------
from docx.shared import Pt
p = doc.add_paragraph()
run = p.add_run("Formatted Text")
run.bold = True
run.font.size = Pt(14)
📑 Read Tables from Document
-------------------------------
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
print(cell.text)
=========================
🔁 SPLIT DOCUMENTS IN CHUNKS and replace names
=========================
from docx import Document
import pandas as pd
# Load Excel file
df = pd.read_excel("data.xlsx")
# Load Word template
doc = Document("template.docx")
# Replace (1 )االسمto (4 )االسمin paragraphs
names = df["]"االسم.tolist()[:4]
for i in range(4):
for p in doc.paragraphs:
p.text = p.text.replace(f"({االسمi+1})", names[i] if i < len(names) else "")
# Save result
doc.save("output.docx")
=========================
🔁 SPLIT DOCUMENTS IN CHUNKS
=========================
for i in range(0, len(df), 4):
doc = Document("template.docx")
group = df.iloc[i:i+4]["]"االسم.tolist()
for j in range(4):
for p in doc.paragraphs:
p.text = p.text.replace(f"({االسمj+1})", group[j] if j < len(group) else
"")
doc.save(f"output_{i//4 + 1}.docx")
=========================
📊 pandas + openpyxl (Excel) Examples
=========================
✅ Read and Write Excel
-------------------------------
import pandas as pd
df = pd.read_excel("data.xlsx")
df.to_excel("new_data.xlsx", index=False)
🔍 Filter and Select Data
-------------------------------
df_filtered = df[df["]"فني شبكات" == ]"الوظيفة
df_subset = df[[" "التاريخ,"]]"االسم
➕ Add/Update Columns and Rows
-------------------------------
df["status"] = "Active"
df.loc[len(df)] = ["12-05-2025" ," "محللة بيانات,""رنا, "Active"]
🔄 Modify Column Values
-------------------------------
df[" = ]"التاريخdf["]"التاريخ.astype(str)
df.at[0, ""مبرمج" = ]"الوظيفة
📑 Sort, Drop, Fill
-------------------------------
df = df.sort_values(")"التاريخ
df = df.drop(columns=["status"])
df = df.fillna("N/A")
🔁 Loop Through Rows
-------------------------------
for index, row in df.iterrows():
print(f"{row[' }]'االسمworks as {row[' }]'الوظيفةon {row[')"}]'التاريخ
📂 Group and Aggregate
-------------------------------
job_counts = df.groupby(")"الوظيفة.size()
print(job_counts)
earliest_date = df.groupby("]"التاريخ"[)"الوظيفة.min()
print(earliest_date)
🧾 Read Excel with Multiple Sheets
-------------------------------
xls = pd.ExcelFile("multi_sheet.xlsx")
df1 = pd.read_excel(xls, "Sheet1")
df2 = pd.read_excel(xls, "Sheet2")
=========================
📘 Project 1: Certificate Generator (Simple)
=========================
Goal: Read a list of student names from Excel and generate personalized Word
certificates.
Excel (students.xlsx):
------------------------
| االسم |
|----------|
| أحمد |
| منى |
| يوسف |
Word Template (certificate_template.docx):
------------------------------------------
This is to certify that ( )االسمhas completed the course.
Python Code:
------------------------------------------
import pandas as pd
from docx import Document
df = pd.read_excel("students.xlsx")
for index, row in df.iterrows():
doc = Document("certificate_template.docx")
name = row["]"االسم
for p in doc.paragraphs:
if "( ")االسمin p.text:
p.text = p.text.replace("(")االسم, name)
doc.save(f"Certificate_{name}.docx")
=========================
📘 Project 2: Staff Report Generator (Moderate)
=========================
Goal: Read employee data from Excel and generate a report table in a Word document.
Excel (employees.xlsx):
------------------------
| | التاريخ | الوظيفة االسم |
|----------|------------|----------------|
| 06-05-2025 | | مهندس نظم عمر |
| 07-05-2025 | | محلل بيانات خالد |
Python Code:
------------------------------------------
import pandas as pd
from docx import Document
df = pd.read_excel("employees.xlsx")
doc = Document()
doc.add_heading("Employee Daily Report", level=1)
table = doc.add_table(rows=1, cols=3)
table.style = "Table Grid"
table.cell(0, 0).text = ""االسم
table.cell(0, 1).text = ""الوظيفة
table.cell(0, 2).text = ""التاريخ
for _, row in df.iterrows():
cells = table.add_row().cells
cells[0].text = str(row[")]"االسم
cells[1].text = str(row[")]"الوظيفة
cells[2].text = str(row[")]"التاريخ
doc.save("Staff_Report.docx")
=========================
📘 Project 3: Multi-Page Offer Letters (Advanced)
=========================
Goal: Read job offers from Excel and generate a multi-paragraph Word document for
each candidate.
Excel (offers.xlsx):
------------------------
| | تاريخ البدء | المسمى الوظيفي | الراتب | االسم
|----------|----------------|----------|-------------|
| 01-07-2025 | ج8000 | | مطور برمجيات | ندى
Word Template (offer_template.docx):
------------------------------------------
Dear ()االسم,
We are pleased to offer you the position of ( )المسمى الوظيفيat our company.
Your starting salary will be ()الراتب, beginning on ()تاريخ البدء.
Best regards,
HR Department
Python Code:
------------------------------------------
import pandas as pd
from docx import Document
df = pd.read_excel("offers.xlsx")
for _, row in df.iterrows():
doc = Document("offer_template.docx")
replacements = {
"(")االسم: row["]"االسم,
"(")المسمى الوظيفي: row["]"المسمى الوظيفي,
"(")الراتب: str(row[")]"الراتب,
"(")تاريخ البدء: str(row[")]"تاريخ البدء
}
for p in doc.paragraphs:
for key, val in replacements.items():
if key in p.text:
p.text = p.text.replace(key, val)
doc.save(f"Offer_Letter_{row['}]'االسم.docx")
=========================
➜Project 4: Student Evaluation Generator (Excel ➜ Word & Feedback in Excel)
=========================
Goal:
- Read student scores from Excel
- Generate personalized Word feedback letters
- Write feedback evaluations back to Excel
Excel (students_scores.xlsx):
------------------------
| | الدرجة | االسم
|--------|--------|
| 90 | خالد |
| 72 | منى |
| 55 | أحمد |
Word Template (feedback_template.docx):
------------------------------------------
)عزيزي (االسم،
لقد حصلت على درجة (الدرجة) في االختبار النهائي.
) (التقييم:تقييمك العام هو
نتمنى لك دوام النجاح والتفوق.
Python Code:
------------------------------------------
import pandas as pd
from docx import Document
df = pd.read_excel("students_scores.xlsx")
def get_evaluation(score):
if score >= 85:
return ""ممتاز
elif score >= 70:
return ""جيد جدًا
elif score >= 50:
return ""مقبول
else:
return ""ضعيف
for i, row in df.iterrows():
name = row["]"االسم
score = row["]"الدرجة
evaluation = get_evaluation(score)
df.at[i, " = ]"التقييمevaluation
doc = Document("feedback_template.docx")
for p in doc.paragraphs:
p.text = p.text.replace("(")االسم, name)
p.text = p.text.replace("(")الدرجة, str(score))
p.text = p.text.replace("(")التقييم, evaluation)
doc.save(f"Feedback_{name}.docx")
df.to_excel("students_scores_updated.xlsx", index=False)
=========================
📎 TIPS & TRICKS
=========================
- Always check encoding if you read/write Arabic data.
- Excel date columns can be converted to strings using: df["date"] =
df["date"].astype(str)
- python-docx does not support editing headers/footers directly.
=========================
📂 FILE FORMATS SUPPORTED
=========================
- .docx for Word files (not .doc)
- .xlsx for Excel files (not .xls)