0

What I want to do is query the fields of a .shp file from an Excel .xlsx file, that is, avoid the repetitive and manual process of copying the attribute table from ArcMap to the working .xlsx file,

For example, query and write the values ​​of this attribute table displayed in arcmap and contained in the VHDissolve.shp file from my Excel VINCULACION.xlsx file

enter image description here enter image description here

Note: The attribute table and the table in Excel have the same fields.

This is a good start apparently

# Set local variables

inTable = outTable

outXLS = VHFolder + "/Vinculacion_S.xlsx" #this is where I had to give the file path and then the file name

Execute TableToExcel

arcpy.TableToExcel_conversion(inTable, outXLS)

ersion(inTable, outXLS)

Annex link with the .shp and .xlsm files https://drive.google.com/file/d/1z7tbOCxQ0Xjl24tCCmfKMpcgnuomdHT-/view?usp=sharing

Briam Ramon
  • 107
  • 4
  • Shapefile isn't an ArcMap format; it was created for ArcView 25 years ago, and is miraculously still in use today in every GIS application. Python tools that read dBase-III+ and write .xls* shouldn't be too hard to find. What have you tried? – Vince Jun 03 '21 at 15:17
  • @Vince I have researched in this forum and I think these posts are a bit close to what I want to do, https://resources.arcgis.com/en/help/main/10.2/index.html#/Table_To_Excel/001200000054000000/ https://gis.stackexchange.com/questions/114005/export-to-shp-with-arcpy-with-query – Briam Ramon Jun 03 '21 at 15:31
  • 2
    So you'd like to place a query on the SHP file, and write the queried results to the excel file? Is this to create a new excel file, or to append to an existing excel file? Can you provide an example query? Also, can you put the code (from your comment) in the body of the question? – Keggering Jun 03 '21 at 17:28
  • 1
    Hi! @Keggering Yes, the idea is to perform a query in the SHP file and write the results of the query in an existing Excel file.

    Note: update the question, attaching the .shp and .xlsm files

    – Briam Ramon Jun 03 '21 at 18:07
  • What makes you think that either ArcMap or ArcPy support the XLSM format? I think ArcGIS Desktop only supports XLS and XLSX. – PolyGeo Jun 03 '21 at 21:11
  • @PolygGeo What I want is to take the data from the attribute table of the .shp file and write it to an .xlsm or .xlsx file through Python.

    I can convert the .xlsm extension into .xls or .xlsx through python if the case requires it. here is an example of how to do it https://www.tutorialspoint.com/How-to-change-file-extension-in-Python

    – Briam Ramon Jun 03 '21 at 21:27
  • I update what I have researched - you probably have to use a library like OpenPyXL to write to the existing Excel file and start with a SearchCursor in the shapefile using a where_clause to query the desired records then loop through the SearchCursor and figure out how to write the rows in Excel. https://openpyxl.readthedocs.io/en/stable/ https://desktop.arcgis.com/es/arcmap/latest/analyze/arcpy-data-access/searchcursor-class.htm https://openpyxl.readthedocs.io/en/stable/usage.html#write-a-workbook – Briam Ramon Jun 03 '21 at 21:32
  • It seems openpyxl is pre-installed in ArcPro (https://imgur.com/a/fSN7mK2). Can this data-processing be handled there? Would prevent having to go through the headache of installing in your ArcMap environment (e.g. https://joelmccune.com/installing-python-packages-on-an-arcgis-python-installation/ ) – Keggering Jun 03 '21 at 23:25
  • 1
    You say that you can write "to an .xlsm or .xlsx file through Python. I can convert the .xlsm extension into .xls or .xlsx through python". That sounds like a pure Python question that should be researched/asked at [so]. – PolyGeo Jun 03 '21 at 23:40

3 Answers3

1

Your code should look something more like this:

import arcpy
import openpyxl as px

def main(): wb = px.load_workbook(r"C:\Downloads\Ejemplo\VINCULACION.S.xlsx") ws = wb["VINCULACION_SH_NUE"] in_features = r"C:\Downloads\Ejemplo\VH_Dissolve.shp"

row_num = 3
with arcpy.da.SearchCursor(
    in_features,
    ["COLOR", "INTERNO_DE", "CLASE_DEMA", "COUNT_AREA", "SUM_AREA", "SUM_LENGTH"],
) as cursor:
    for row in cursor:
        ws.cell(row=row_num, column=2).value = row[0]
        ws.cell(row=row_num, column=3).value = row[1]
        ws.cell(row=row_num, column=4).value = row[2]
        ws.cell(row=row_num, column=6).value = row[3]
        ws.cell(row=row_num, column=7).value = row[4]
        ws.cell(row=row_num, column=8).value = row[5]
        row_num += 1
wb.save(r"C:\Downloads\Ejemplo\VINCULACION_v2.xlsx")


if name == "main": main()

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Mark Bryant
  • 1,435
  • 8
  • 7
  • Thanks to @MarkBryant great solution and I can use it for many more cases of handling to .shp to excel (.xls / .xlsx / .xlsm).

    Note: to use the code in an excel file with macros .xlsm it must be modified to line 4 like this: wb = px.load_workbook(r"C:\Users\Hp\Desktop\Ejemplo\VINCULACION_S.xlsm", read_only=False, keep_vba=True)

    – Briam Ramon Jun 08 '21 at 14:50
0

The ArcMap Table To Excel tool:

converts only to Microsoft Excel 5.0/95 Workbook (.xls) format.

The ArcGIS Pro Table To Excel tool:

Converts a table to a Microsoft Excel file (.xls or .xlsx).

Neither of these two applications in the ArcGIS Desktop product write to .xlsm.

There is no mention of .xlsm in either of these two Esri support pages:

Consequently, any conversion from .xls or .xlsx to .xlsm will need to be performed outside of ArcGIS Desktop. Since you are looking to do this using Python I think any question about that will need to be researched/asked at Stack Overflow.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
  • I need to take the table attributes from the .shp file displayed in an .mxd (ArcMap) to take them to an .xlsm file but not exporting them if not as a query. through Python or other programming means. In the previous comments when I referred to what I could do the conversion from .xlsm to .xlsx or .xls it is because it was part of my research, through this example https://www.tutorialspoint.com/How-to-change-file-extension-in-Python – Briam Ramon Jun 04 '21 at 04:02
  • Also in the previous comments write down the probable process to carry out the answers to this question that I publish, starting with a SearchCursor https://pro.arcgis.com/es/pro-app/latest/arcpy/data-access/searchcursor-class.htm – Briam Ramon Jun 04 '21 at 04:05
  • @BriamRamon If you are saying that your question is not about writing data from ArcMap to XLSM using ArcPy, and if you are also saying that you already know how to convert XLS/XLSX to XLSM using Python, then I think you need to start a new question that does not mention XLSM and focuses on where you are actually stuck. – PolyGeo Jun 04 '21 at 04:20
-1

This could be the solution to the post, I still can't integrate Opelpyxl to Python from ArcMap, or Arcpy to IDLE from Python

import arcpy
from openpyxl import Workbook
wb = Workbook("C:/Users/Hp/Desktop/ejemplo/VINCULACION_S.xlsx")
ws =  wb[('VH_NUE']
rows = arcpy.SearchCursor("C:/Users/Hp/Desktop/ejemplo/VH_Dissolve.shp",
                          fields="COLOR; INTERNO_DE; CLASE_DEMA; COUNT_AREA; SUM_AREA; SUM_LENGTH",
                          sort_fields="COLOR 222; INTERNO_DE A")
# COLOR, INTERNO_DE, CLASE_DEMA, COUNT_AREA, SUM_AREA y SUM_LENGTH.
for row in rows:
    ws.append("Color: {0}, Interno: {1}, Clase:{2}, ContarA: {3}, SumarA: {4}, SumarL: {5}".format(
        row.getValue("COLOR"),
        row.getValue("INTERNO_DE"),
        row.getValue("CLASE_DEMA"),
        row.getValue("COUNT_AREA"),
        row.getValue("SUM_AREA"),
        row.getValue("SUM_LENGTH")))
wb.save(VINCULACION_S.xlsx)
Briam Ramon
  • 107
  • 4