Write feature class properties to a Word document using python-docx

It’s been too long since I’ve posted some code. A few months ago I had a requirement to basically create a UML view of a file gdb and present each feature class as tables for inclusion in a functional specification Word document.

Sadly, since the demise of ArcGIS Diagrammer, there has never been something to take its place. The actual request I received was to take screenshots of the properties dialog box of each feature class in ArcCatalog, and paste those into the document with appropriate headings.

I recognised this request for the total waste of my time it would be, and promptly set about looking for an alternative. I realised I had python-docx already installed.

@date 15/09/2015
@author Cindy Williams
Uses the python-docx package to write various
properties of a feature class to docx format.
import arcpy
from docx import Document
arcpy.env.workspace = r"C:\Some\Arb\Folder\work.gdb"
doc = r"C:\Some\Arb\Folder\test.docx"
document = Document()
document.add_paragraph("This document contains a description of fields per feature class.")
for fc in arcpy.ListFeatureClasses():
p = document.add_paragraph("Feature class: ")
p.add_run(fc).bold = True
table = document.add_table(rows=1, cols=2, style='Table Grid')
header_cells = table.rows[0].cells
header_cells[0].text = "Field Name"
header_cells[1].text = "Field Type"
for field in arcpy.ListFields(fc):
row_cells = table.add_row().cells
row_cells[0].text = field.name
row_cells[1].text = field.type
document.add_page_break() # Each feature class on its own page

For each feature class in the geodatabase, a new paragraph is started with the name of the feature class in bold. A line break is inserted, followed by a table. The name and type of each field is added as a new row into the table, and a page break is inserted to start the properties of the next feature class on a new page.

It took me about an hour to look up alternative methods and to put this script together. Most of the time was spent on getting the cells in the table to insert properly, using the age-old method of trial and error. I did it this way to save myself the pain of manually inserting screenshots, knowing that if the format of the feature classes changed I would have to do new screenshots repeatedly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.