Skip to content

Instantly share code, notes, and snippets.

@srinivas946
Created March 7, 2020 16:21
Show Gist options
  • Select an option

  • Save srinivas946/2b38c35b5f5b107759c60de61f0973bb to your computer and use it in GitHub Desktop.

Select an option

Save srinivas946/2b38c35b5f5b107759c60de61f0973bb to your computer and use it in GitHub Desktop.
Interact with Microsoft Office Word Document Using Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Interact with Microsoft Word Document</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Add Heading</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from docx import Document # from docx package import Document Class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create an object for Document class\n",
"# It means you are creating a virtual empty word document\n",
"doc = Document()\n",
"\n",
"# add headings to the document based on it levels\n",
"doc.add_heading('Heading-1', level=0)\n",
"doc.add_heading('Heading-2', level=1)\n",
"doc.add_heading('Heading-3', level=2)\n",
"doc.add_heading('Heading-4', level=3)\n",
"doc.add_heading('Heading-5', level=4)\n",
"doc.add_heading('Heading-6', level=5)\n",
"doc.add_heading('Heading-7', level=6)\n",
"\n",
"# save the document by providing the file path\n",
"# It means virtaul Document resolves to real time document\n",
"doc.save('my_doc.docx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Add Paragraph</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from docx import Document # from docx package import Document Class\n",
"from docx.shared import RGBColor # import RGBColor format for the font\n",
"from docx.shared import Pt # import Pt for font size adjustments"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create an object for Document class\n",
"# It means you are creating a virtual empty word document\n",
"doc = Document()\n",
"\n",
"# different paragraphs styles\n",
"doc.add_paragraph('This a demo paragraph', style='List Paragraph')\n",
"doc.add_paragraph('Second Paragraph with Quote Style', style='Quote')\n",
"\n",
"# add bold text\n",
"para1 = doc.add_paragraph()\n",
"run1 = para1.add_run()\n",
"run1.text = 'Bold Text'\n",
"run1.bold = True\n",
"\n",
"# add paragraph with font name, font size and font size\n",
"para2 = doc.add_paragraph()\n",
"run2 = para2.add_run()\n",
"run2.text = 'Verdana Font'\n",
"run2.font.name = 'verdana'\n",
"run2.font.color.rgb = RGBColor(66, 36, 233)\n",
"run2.font.size = Pt(18)\n",
"\n",
"doc.save('generate_word.docx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Add Picture</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from docx import Document # from docx package import Document Class\n",
"from docx.shared import Inches # add inches to adjust the size of the image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create an object for Document class\n",
"# It means you are creating a virtual empty word document\n",
"doc = Document()\n",
"\n",
"# add heading to the image\n",
"doc.add_heading('Attaching Image', level=2)\n",
"\n",
"# add picture to the document\n",
"doc.add_picture('My_Logo.png', width=Inches(1.2), height=Inches(1.2))\n",
"\n",
"doc.save('image_doc.docx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Add Table</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from docx import Document # from docx package import Document Class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create an object for Document class\n",
"# It means you are creating a virtual empty word document\n",
"doc = Document()\n",
"\n",
"#add table to the document\n",
"table = doc.add_table(rows=2, cols=2)\n",
"table.style = \"Table Grid\" # adding style to table\n",
"\n",
"row1 = table.rows[0] # row one\n",
"cell1 = row1.cells[0] # row one - cell one\n",
"cell1.text = \"Cell One\"\n",
"cell2 = row1.cells[1] # row two - cell two\n",
"cell2.text = \"Cell Two\"\n",
"\n",
"row2 = table.rows[1] # row two\n",
"cell1 = row2.cells[0] # row two - cell one\n",
"cell1.text = \"Cell Three\"\n",
"cell2 = row2.cells[1] # row two - cell two\n",
"cell2.text = \"Cell Four\"\n",
"\n",
"doc.save('table_doc.docx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Learn Real Time Scenarios related to Word Document in Cyber Security Daily Tasks visit : <a href=\"https://cybersecpy.in/interact-with-microsoft-office-services-python/\">cybersecpy</a></b>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment