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.
|@author Cindy Williams|
|Uses the python-docx package to write various|
|properties of a feature class to docx format.|
|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.cells|
|header_cells.text = "Field Name"|
|header_cells.text = "Field Type"|
|for field in arcpy.ListFields(fc):|
|row_cells = table.add_row().cells|
|row_cells.text = field.name|
|row_cells.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.