Skip to main content
Crowdfunding
Python + AI for Geeks
Practice

Challenge! 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 generation tab.

Using the python-docx library, write a program that creates a 3x3 table and fills each cell with data.

Creating a Simple Table
from docx import Document

def create_simple_table():
# Write your code here
pass

Requirements

  1. Create a new Word document.

  2. Add a 3-row, 3-column table to the document.

  3. Set the table borders to solid lines.

  4. Fill each cell of the table with the following data:

Table Data
First row: "Name", "Age", "Occupation"

Second row: "John Smith", "30", "Developer"

Third row: "Jane Doe", "25", "Designer"
  1. 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:

NameAgeOccupation
John Smith30Developer
Jane Doe25Designer

The specified data should be correctly entered into each cell of the table.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.