Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
Generating Word templates from Excel spreadsheets using Python offers numerous benefits that can significantly enhance efficiency, accuracy, and presentation in various professional and personal contexts. By leveraging automation, customization, and data integrity, professionals can ensure their Word documents are effective communication tools that convey essential information clearly and accurately. One such library that can convert Microsoft Excel to Microsoft Word is the IronXL Python package from Iron Software and the python-docx library.
This article will examine the steps required to generate Word documents from Excel files.
excelToWord.py
.IronXL for Python is a robust library developed by Iron Software that allows developers to create, read, and edit Excel files (XLS, XLSX, and CSV) in Python projects. Here are some key features and benefits of using IronXL:
python-docx is a Python library that creates, modifies, and works with Microsoft Word documents such as .docx
files. It provides a simple API to interact with Word documents, allowing you to perform tasks such as adding text, formatting, inserting tables and images, and more.
You can generate Word documents from scratch and add content, including paragraphs, tables, headings, and more. This package can also be used to edit individual documents.
Add and modify paragraphs of text. Format text (e.g., bold, italic, underline, etc.) using "runs" (parts of text with different styles within a paragraph). Add and style headings of various levels.
Create tables with a specified number of rows and columns. Access and modify individual cells in a table.
Create bulleted or numbered lists with predefined styles.
Apply predefined styles like "Heading 1", "Normal", etc. You can also define and apply custom styles to paragraphs or text.
Insert images into the document at specific locations. You can resize images by specifying their width and height.
Before we dive into the code, ensure that you have the following prerequisites:
excelToWord.py
Open your favorite IDE like Visual Studio Code and create a file called excelToWord.py
.
Use Pip to install the IronXL and python-docx packages.
pip install IronXL python-docx
pip install IronXL python-docx
Copy the sample Excel file to your code folder. The file contains the below data.
Using IronXL, load the Excel document and read all the cells using the code below.
import ironxl
# Import Document class from python-docx to work with Word documents
from docx import Document
# Set the License Key for IronXL (replace 'your license' with your actual license key)
ironxl.License.LicenseKey = "your license"
# Load the Excel workbook and select the first worksheet
workbook = ironxl.WorkBook.Load("sample.xlsx")
sheet = workbook.WorkSheets[0]
# Read data from the Excel sheet
data = []
# Iterate through rows and columns in the Excel sheet
for row in range(0, len(sheet.Rows)):
row_data = []
for col in range(0, len(sheet.Columns)):
cell_value = sheet.GetCellAt(row, col)
print(cell_value) # Print each cell value
row_data.append(cell_value)
data.append(row_data)
import ironxl
# Import Document class from python-docx to work with Word documents
from docx import Document
# Set the License Key for IronXL (replace 'your license' with your actual license key)
ironxl.License.LicenseKey = "your license"
# Load the Excel workbook and select the first worksheet
workbook = ironxl.WorkBook.Load("sample.xlsx")
sheet = workbook.WorkSheets[0]
# Read data from the Excel sheet
data = []
# Iterate through rows and columns in the Excel sheet
for row in range(0, len(sheet.Rows)):
row_data = []
for col in range(0, len(sheet.Columns)):
cell_value = sheet.GetCellAt(row, col)
print(cell_value) # Print each cell value
row_data.append(cell_value)
data.append(row_data)
The Word document generation process involves creating a Word document and inserting data that was read from the Excel file.
# Create a new Word document
doc = Document()
# Add a title to the Word document
doc.add_heading('Excel Data Export Using Python Docx', 0)
# Create a table with headers (first row of Excel data)
table = doc.add_table(rows=1, cols=len(data[0]))
hdr_cells = table.rows[0].cells
# Populate header cells with data
for i, header in enumerate(data[0]):
hdr_cells[i].text = str(header)
# Populate table with data from Excel
for row in data[1:]:
row_cells = table.add_row().cells
for i, cell in enumerate(row):
row_cells[i].text = str(cell)
# Save the generated Word document
doc.save("sample.docx")
# Create a new Word document
doc = Document()
# Add a title to the Word document
doc.add_heading('Excel Data Export Using Python Docx', 0)
# Create a table with headers (first row of Excel data)
table = doc.add_table(rows=1, cols=len(data[0]))
hdr_cells = table.rows[0].cells
# Populate header cells with data
for i, header in enumerate(data[0]):
hdr_cells[i].text = str(header)
# Populate table with data from Excel
for row in data[1:]:
row_cells = table.add_row().cells
for i, cell in enumerate(row):
row_cells[i].text = str(cell)
# Save the generated Word document
doc.save("sample.docx")
# Import required libraries
import ironxl
from docx import Document
# Set the License Key for IronXL
ironxl.License.LicenseKey = "your license"
# Load the Excel workbook
workbook = ironxl.WorkBook.Load("sample.xlsx")
sheet = workbook.WorkSheets[0]
# Read data from the Excel sheet
data = []
# Iterate through rows and columns in the Excel sheet
for row in range(0, len(sheet.Rows)):
row_data = []
for col in range(0, len(sheet.Columns)):
cell_value = sheet.GetCellAt(row, col)
print(cell_value) # Print each cell value
row_data.append(cell_value)
data.append(row_data)
# Document generation process
# Create a new Word document
doc = Document()
# Add a title to the Word document
doc.add_heading('Excel Data Export Using Python Docx', 0)
# Create a table in the Word document
table = doc.add_table(rows=1, cols=len(data[0]))
hdr_cells = table.rows[0].cells
for i, header in enumerate(data[0]):
hdr_cells[i].text = str(header) # Add header cells
for row in data[1:]:
row_cells = table.add_row().cells
for i, cell in enumerate(row):
row_cells[i].text = str(cell)
# Save the Word document
doc.save("sample.docx")
# Import required libraries
import ironxl
from docx import Document
# Set the License Key for IronXL
ironxl.License.LicenseKey = "your license"
# Load the Excel workbook
workbook = ironxl.WorkBook.Load("sample.xlsx")
sheet = workbook.WorkSheets[0]
# Read data from the Excel sheet
data = []
# Iterate through rows and columns in the Excel sheet
for row in range(0, len(sheet.Rows)):
row_data = []
for col in range(0, len(sheet.Columns)):
cell_value = sheet.GetCellAt(row, col)
print(cell_value) # Print each cell value
row_data.append(cell_value)
data.append(row_data)
# Document generation process
# Create a new Word document
doc = Document()
# Add a title to the Word document
doc.add_heading('Excel Data Export Using Python Docx', 0)
# Create a table in the Word document
table = doc.add_table(rows=1, cols=len(data[0]))
hdr_cells = table.rows[0].cells
for i, header in enumerate(data[0]):
hdr_cells[i].text = str(header) # Add header cells
for row in data[1:]:
row_cells = table.add_row().cells
for i, cell in enumerate(row):
row_cells[i].text = str(cell)
# Save the Word document
doc.save("sample.docx")
This Python script performs two main tasks.
sample.xlsx
) and selects the first worksheet from the file.sample.docx
.The script reads data from an Excel file (sample.xlsx
), processes it, and exports the data into a table in a new Word document (sample.docx
). The first row of the Excel sheet is used as table headers, and each row of data from the Excel sheet is added to the Word document as a row in the table.
IronXL works on a valid license file attached to the code. Users can easily get a trial license from the license page.
To use the license, place the license key somewhere in the code as below before using the IronXL library.
ironxl.License.LicenseKey = "Your License Key"
ironxl.License.LicenseKey = "Your License Key"
The sample code demonstrates an effective way to read data from an Excel file using IronXL and then export that data into a Word document using python-docx. The process involves two main steps:
This approach allows for seamless data transfer from Excel to Word, which can be useful for tasks such as report generation, data exports, or document automation. The combination of IronXL for Excel handling and python-docx for Word document creation provides a powerful solution for working with these file formats in Python.
IronXL for Python is a robust library developed by Iron Software that allows developers to create, read, and edit Excel files (XLS, XLSX, and CSV) in Python projects without requiring Microsoft Excel installed on the server.
Key features of IronXL include no Excel dependency, an intuitive API, support for multiple formats like XLS, XLSX, CSV, cell styling options, formula handling, and cross-platform compatibility.
Python-docx is a Python library that creates, modifies, and works with Microsoft Word documents such as .docx files, allowing for tasks like adding text, formatting, and inserting tables and images.
To generate a Word document from Excel data using Python, you need to read the Excel file with IronXL, create a Word document with python-docx, and insert the Excel data into the Word document by creating a table.
Prerequisites include having Python installed, and installing both IronXL and python-docx packages via pip. You also need a sample Excel file with data for conversion.
The process involves setting up a license for IronXL, loading the Excel file, selecting the worksheet, and iterating through rows and columns to read cell values into a data structure.
Using python-docx, create a new Word document, add a title, create a table, and populate it with data from Excel. The first row from Excel is used as headers in the Word table.
You can install IronXL and python-docx using pip with the command 'pip install IronXL python-docx'.
IronXL does not require Microsoft Excel to be installed on the server, offers an intuitive API, supports multiple Excel formats, and provides features like cell styling and formula handling.
Python-docx provides a simple API to create and manipulate Word documents, allowing for tasks such as adding text, formatting, inserting tables, and more, making it ideal for document automation.