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

9
  • 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
    Commented Jun 3, 2021 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, resources.arcgis.com/en/help/main/10.2/index.html#/… gis.stackexchange.com/questions/114005/… Commented Jun 3, 2021 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
    Commented Jun 3, 2021 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 Commented Jun 3, 2021 at 18:07
  • 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 Stack Overflow.
    – PolyGeo
    Commented Jun 3, 2021 at 23:40

3 Answers 3

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()
1
  • 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) Commented Jun 8, 2021 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.

3
  • 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 tutorialspoint.com/How-to-change-file-extension-in-Python Commented Jun 4, 2021 at 4: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 pro.arcgis.com/es/pro-app/latest/arcpy/data-access/… Commented Jun 4, 2021 at 4: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
    Commented Jun 4, 2021 at 4: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)

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