0

I am using ArcPy to select from a layer and display the result on map as a table view.

Now I want to export the table view as a PDF format.

Anybody know about it?

If it is beyond ArcPy to export as PDF directly, with intermediate format also welcome.

  • It sounds like you already know how to do 1 and 2 so I think you should edit them out of your Question so that it simply focuses on how to export a table to one format, perhaps PDF, first. What format is the table you want converted to PDF in? If it is a Table View then let us know if you are happy for any intermediate format to be used. Once this question resolves PDF export you can research/ask about the others separately. – PolyGeo May 28 '14 at 7:06
  • Just double-checking whether when you say "on map as a table view" you are thinking of a layout element? A Table View is the table equivalent of a Feature Layer for a feature class. My thinking has been to use Table To Table to get a dBASE table and then look for a 3rd party Python library to convert dBase to PDF. – PolyGeo May 30 '14 at 4:36
  • Hi PolyGeo, the table I mentioned is a table on top of the map, it is something like the attribute table. Thanks. – ppLily May 30 '14 at 5:36
  • note that you can create a table within arcpy.mapping. This is a bit of work because you will need to loop through the values and place them correctly, but it is possible. have a look at gis.stackexchange.com/questions/81986/… – radouxju May 30 '14 at 18:45
1

You may be able to put something together with ReportLab. There is a sample script that uses ReportLab to make a set of page indexes for a mapbook on arcgis.com that may be useful as a reference.

1

For both (2) and (3), arcpy.TableToTable_conversion() is what you're looking for. You can export the selected attribute table records of the feature layer in (1) to a geodatabase or the in_memory workspace (from where it can be added to the ArcMap document), and subsequently export either that table or the original selection on the feature layer to the output format of your choice.

Exporting a table to PDF is not an option with any part of arcpy that I'm aware of.

  • thanks nmpeterson for your reply, I will follow it , if pdf cannot exported by using arcpy, then I need to look into ArcObjects. – ppLily May 28 '14 at 4:04
1

I have done something similar using both what was suggested by @nmpeterson and @Jason, here is a basic Platypus (ReportLab) sample with a table:

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch
from reportlab.lib import colors

import arcpy

PAGE_HEIGHT=defaultPageSize[1]
PAGE_WIDTH=defaultPageSize[0]
styles = getSampleStyleSheet()
Title = "Table"
pageinfo = "Table example"

def myFirstPage(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Bold',16)
    canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title)
    canvas.setFont('Times-Roman',9)
    canvas.drawString(inch, 0.75 * inch,"First Page / %s" % pageinfo)
    canvas.restoreState()

def myLaterPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman', 9)
    canvas.drawString(inch, 0.75 * inch,"Page %d %s" % (doc.page, pageinfo))
    canvas.restoreState()

def go():

    # Convert your table to an array with whatever method works best for you
    dataDic = tableToDic(tablePath)
    columnName = dataDic.keys()
    dataArray = dicToArray(dataDic)

    # Create the ReportLab document
    doc = SimpleDocTemplate("helloTable.pdf")

    # Create the table element with whatever parameters suit you best
    Story = [Spacer(1,2*inch)]
    style = styles["Normal"]
    dataArray = tableArray
    width = len(columnNames)
    dataArray.insert(0, columnNames)
    cellWidth = [7 * inch / width for i in columnNames]
    table = Table(dataArray, cellWidth)
    table.setStyle(TableStyle([
        ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
        ('GRID',(0,0),(-1,-1),0.5,colors.black),
    ]))

    # Generate the document
    Story.append(table)
    doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)

if __name__ == "__main__":
    go()

About the "convert the table to an array" part, it depends on what ArcGIS version you are using. I have discussed a similar issue on the thread : Is it a bad idea to convert a File Geodatabase table to a dictionary? If you are using ArcGIS 10.1+, you can use nmpeterson's make_attribute_dict function and customize it to make an array instead of a dictionary. If you're using an older version, I can give you some other pieces of code to convert your tables to a dictionary/array. If you use the table to dictionary function such as, you can also use the following line to convert the dictionary to an array:

tableArray = [tuple(tableDic[key]) for key in tableDic.iterkeys()]

Of course you can always turn things around to work directly with arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.