Creating Word and Excel documents using Python can be a useful skill to have, whether you are creating reports, generating invoices, or automating tasks. In this tutorial, we will walk through how to use the python-docx
library to create Word documents and the openpyxl
library to create Excel documents.
Step 1: Install the necessary libraries
Before we can start creating Word and Excel documents with Python, we need to install the required libraries. Open up your command line or terminal and run the following commands:
pip install python-docx
pip install openpyxl
Step 2: Creating a Word document
Now that we have the required libraries installed, let’s start by creating a Word document using the python-docx
library. Create a new Python script and include the following code:
from docx import Document
# Create a new Word document
doc = Document()
# Add a title to the document
doc.add_heading('Hello, World!', level=1)
# Add some text to the document
doc.add_paragraph('This is a simple Word document created using Python.')
# Save the document
doc.save('example.docx')
In this code snippet, we first import the Document
class from the docx
module. We then create a new Word document, add a title and some text to it, and finally save the document as example.docx
in the current directory.
Step 3: Creating an Excel document
Next, let’s move on to creating an Excel document using the openpyxl
library. Add the following code to your Python script:
from openpyxl import Workbook
# Create a new Excel workbook
wb = Workbook()
# Get the active worksheet
ws = wb.active
# Add some data to the worksheet
ws.append(['Name', 'Age', 'Occupation'])
ws.append(['John Doe', 30, 'Engineer'])
ws.append(['Jane Smith', 25, 'Designer'])
# Save the workbook
wb.save('example.xlsx')
In this code snippet, we import the Workbook
class from the openpyxl
module and create a new Excel workbook. We then get the active worksheet, add some headers and data to it, and save the workbook as example.xlsx
in the current directory.
Step 4: Running the script
To run the script and create the Word and Excel documents, simply save the Python script with a .py
extension and execute it using your preferred Python interpreter. You should see two new files, example.docx
and example.xlsx
, generated in the current directory.
Congratulations! You have successfully created Word and Excel documents using Python. Feel free to customize the content and formatting of the documents based on your needs and explore more advanced features of the python-docx
and openpyxl
libraries. Happy coding!
siempre con buen contenido
Como idea muy buena, pero con un ejemplo lo entenderÃa mejor, creo un ejercicio práctico es más didáctico.
Yo creo que lo mejor es usar algo más universal como md, html + css + js o látex.
Docx al final es un xml con formato más y estas limitado al software de Microsoft Office o LibreOffice con odt u ods
los blog PERSONALES
lo que NO se comparte
se pierde
eso lo saben bien, los del Rincón del vago
de las grandes multinacionales
saludos cordiales