Challenge Yourself! Creating a Simple Table with python-docx
In this task, you will write a program to create a simple table using the python-docx library.
If you find it difficult to write the code yourself, you can get help from AI in the
Code generationtab.
Using the python-docx library, write a program that creates a 3x3 table and fills each cell with data.
from docx import Document def create_simple_table(): # Write your code here pass
Requirements
-
Create a new Word document.
-
Add a 3-row, 3-column table to the document.
-
Set the table borders to solid lines.
-
Fill each cell of the table with the following data:
First row: "Name", "Age", "Occupation" Second row: "John Smith", "30", "Developer" Third row: "Jane Doe", "25", "Designer"
- Save the document as 'output_file.docx'.
Hints
-
You can create a new Word document using the
Document()object. -
You can add a table using the
add_table()method. -
To access each cell of the table, use the format
table.cell(row, column).text = "content". -
To save the Word document, use
save('output_file.docx').
Based on what you've learned so far, write a program that creates a simple table and make sure it meets all the requirements.
Click the Model Answer button at the bottom right to see an example of how to write the function.
Expected Result
When you open the generated output_file.docx file, you should see a 3x3 table with the following structure:
| Name | Age | Occupation |
|---|---|---|
| John Smith | 30 | Developer |
| Jane Doe | 25 | Designer |
The specified data should be correctly entered into each cell of the table.
Lessons in this chapter ยท How to automate repetitive PowerPoint and Word tasks using Python code
- 1. How to Automate Repetitive PowerPoint Tasks
- 2. Adding Text Box and Text to Slides
- 3. Changing Font, Size, and Color of Text Formatting
- 4. Inserting and Adjusting Images on a Slide
- 5. Creating and Managing Tables in Slides
- 6. Inserting Excel Data into a Slide as a Table
- 7. Creating Charts and Visualizing Data with Python
- 8. Fill-in-the-blank quiz
- 9. Create Word Documents with Python Code
- 10. Adding and Formatting Paragraphs
- 11. Creating Lists
- 12. How to Create Tables with Python Code
- 13. Inserting and Styling Images in Your Document
- 14. Adjusting Document Page Layout
- 15. Read Excel with openpyxl & Write to Word
- 16. Multiple-choice quiz
- 17. Challenge Yourself! Creating a Simple Table with python-docx
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help