查询.shp文件到Excel时出错如何解决?

How to solve error when querying the .shp file to Excel?

我正在用 geopandas 和 xlwings 制作一个 Python 代码,我所做的是来自 Excel 的 colsulse .shp 文件,它工作正常但是当它到达代码的最后一行时 wb.save (in_file) 应该保存的位置 Excel 工作簿,Excel 意外重启或关闭,我不知道我做错了什么,除了如果我删除该行并尝试手动保存 Excel 工作簿时,发生了同样的事情。

密码是:

import geopandas as gpd
import pandas as pd
import xlwings as xw
from pathlib import Path

#VINCULACION_S
in_file = str(Path(__file__).parent / Path("VINCULACION_S.xlsm"))
wb = xw.Book(in_file)

#VINCULACION_SH_NUE
sht1 = wb.sheets['VINCULACION_SH_NUE']
in_features1 = str(Path(__file__).parent / Path("INSUMOS/VH_NUE.shp"))
shp1 = gpd.GeoDataFrame.from_file(in_features1, ignore_geometry=True, index=False)
shp1 = shp1[['COLOR', 'INTERNO_DE', 'CLASE_DEMA', 'COUNT_AREA', 'SUM_AREA', 'SUM_LENGTH']]
shp1.insert(3, "CIV", '', allow_duplicates=False)

urow1 = sht1.range('B2').end('down').last_cell.row
sht1.range("5" + ":" + str(urow1)).clear_contents()
sht1.range('B2').options(pd.DataFrame, index=False).value = shp1

#VINCULACION_SH_ACT
sht2 = wb.sheets['VINCULACION_SH_ACT']
in_features2 = str(Path(__file__).parent / Path("INSUMOS/VH_ACT.shp"))
shp2 = gpd.GeoDataFrame.from_file(in_features2, ignore_geometry=True, index=False)
shp2 = shp2[['COLOR', 'INTERNO_DE', 'CLASE_DEMA', 'COUNT_AREA', 'SUM_AREA', 'SUM_LENGTH']]

urow2 = sht2.range('C2').end('down').last_cell.row
sht2.range("5" + ":" + str(urow2)).clear_contents()
sht2.range('C2').options(pd.DataFrame, index=False).value = shp2

#VINCULACION_SH_CIV
sht3 = wb.sheets['VINCULACION_SH_CIV']
in_features3 = str(Path(__file__).parent / Path("INSUMOS/VH_CIV.shp"))
shp3 = gpd.GeoDataFrame.from_file(in_features3, ignore_geometry=True, index=False)
shp3 = shp3[['INTERNO_DE', 'CLASE_DEMA', 'MVICIV']]

urow3 = sht3.range('B2').end('down').last_cell.row
sht3.range("5" + ":" + str(urow3)).clear_contents()
sht3.range('B2').options(pd.DataFrame, index=False).value = shp3

#VINCULACION_SV_NUE
sht4 = wb.sheets['VINCULACION_SV_NUE']
in_features4 = str(Path(__file__).parent / Path("INSUMOS/VV.shp"))
shp4 = gpd.GeoDataFrame.from_file(in_features4, ignore_geometry=True, index=False)
shp4 = shp4[['INTERNO_SE', 'TIPO_SENAL', 'FASE_ACCIO', 'CONTENIDO1', 'CONTENIDO2', 'VELOCIDAD']]
shp4.insert(2, "CIV", '', allow_duplicates=False)

urow4 = sht4.range('B2').end('down').last_cell.row
sht4.range("5" + ":" + str(urow4)).clear_contents()
sht4.range('B2').options(pd.DataFrame, index=False).value = shp4

#VINCULACION_SV_ACT
sht5 = wb.sheets['VINCULACION_SV_ACT']
in_features5 = str(Path(__file__).parent / Path("INSUMOS/VV_ACT.shp"))
shp5 = gpd.GeoDataFrame.from_file(in_features5, ignore_geometry=True, index=False)
shp5 = shp5[['INTERNO_SE', 'TIPO_SENAL', 'FASE_ACCIO']]

urow5 = sht5.range('C2').end('down').last_cell.row
sht5.range("5" + ":" + str(urow5)).clear_contents()
sht5.range('C2').options(pd.DataFrame, index=False).value = shp5

#VINCULACION_SV_CIV
sht6 = wb.sheets['VINCULACION_SV_CIV']
in_features6 = str(Path(__file__).parent / Path("INSUMOS/VV_CIV.shp"))
shp6 = gpd.GeoDataFrame.from_file(in_features6, ignore_geometry=True, index=False)
shp6 = shp6[['INTERNO_SE', 'TIPO_SENAL', 'MVICIV']]

urow6 = sht6.range('B2').end('down').last_cell.row
sht6.range("5" + ":" + str(urow6)).clear_contents()
sht6.range('B2').options(pd.DataFrame, index=False).value = shp6

wb.save(in_file)

在意外重启或关闭后抛出这个错误 Excel

Traceback (most recent call last):
  File "C:\Users\Hp\Desktop\Ejemplo\VINCULACION_S.py", line 72, in <module>
    wb.save(in_file)
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\main.py", line 740, in save
    self.impl.save(path)
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 552, in save
    self.xl.SaveAs(os.path.realpath(path), FileFormat=file_format)
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python39\lib\site-packages\xlwings\_xlwindows.py", line 66, in __call__
    v = self.__method(*args, **kwargs)
  File "C:\Users\Hp\AppData\Local\Temp\gen_py.9[=12=]020813-0000-0000-C000-000000000046x0x1x9.py", line 46533, in SaveAs
    return self._oleobj_.InvokeTypes(3174, LCID, 1, (24, 0), ((12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (3, 49), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17), (12, 17)),Filename
pywintypes.com_error: (-2147023170, 'Error en la llamada a procedimiento remoto.', None, None)

Update: 到目前为止,我尝试过的是将 shp 和 sht 的每个过程的代码分开,即是,我在不同的 .py selecting 中执行 excel sheet 结果将到达的位置并且以这种方式它做得很好,我正在尝试向 python 代码在每个 # ... select excel sheet 中处理并继续直到保存。

显然错误发生是因为代码中有两个或多个等级的工作表被激活,我通过在每个# ... sht = wb.sheets ['name_of_the_sheet_to_process']中添加代码解决了这个问题。激活()

我分享了结果和功能代码,我希望有人会发现这个 post 有用

import geopandas as gpd
import pandas as pd
import xlwings as xw
from pathlib import Path

#VINCULACION_S
in_file = str(Path(__file__).parent / Path("VINCULACION_S.xlsm"))
wb = xw.Book(in_file)

#VINCULACION_SH_NUE
sht1 = wb.sheets['VINCULACION_SH_NUE'].activate()
sht1 = wb.sheets['VINCULACION_SH_NUE']
in_features1 = str(Path(__file__).parent / Path("INSUMOS/VH_NUE.shp"))
shp1 = gpd.GeoDataFrame.from_file(in_features1, ignore_geometry=True, index=False)
shp1 = shp1[['COLOR', 'INTERNO_DE', 'CLASE_DEMA', 'COUNT_AREA', 'SUM_AREA', 'SUM_LENGTH']]
shp1.insert(3, "CIV", '', allow_duplicates=False)

urow1 = sht1.range('B2').end('down').last_cell.row
sht1.range("5" + ":" + str(urow1)).clear_contents()
sht1.range('B2').options(pd.DataFrame, index=False).value = shp1

#VINCULACION_SH_ACT
sht2 = wb.sheets['VINCULACION_SH_ACT'].activate()
sht2 = wb.sheets['VINCULACION_SH_ACT']
in_features2 = str(Path(__file__).parent / Path("INSUMOS/VH_ACT.shp"))
shp2 = gpd.GeoDataFrame.from_file(in_features2, ignore_geometry=True, index=False)
shp2 = shp2[['COLOR', 'INTERNO_DE', 'CLASE_DEMA', 'COUNT_AREA', 'SUM_AREA', 'SUM_LENGTH']]

urow2 = sht2.range('C2').end('down').last_cell.row
sht2.range("5" + ":" + str(urow2)).clear_contents()
sht2.range('C2').options(pd.DataFrame, index=False).value = shp2

#VINCULACION_SH_CIV
sht3 = wb.sheets['VINCULACION_SH_CIV'].activate()
sht3 = wb.sheets['VINCULACION_SH_CIV']
in_features3 = str(Path(__file__).parent / Path("INSUMOS/VH_CIV.shp"))
shp3 = gpd.GeoDataFrame.from_file(in_features3, ignore_geometry=True, index=False)
shp3 = shp3[['INTERNO_DE', 'CLASE_DEMA', 'MVICIV']]

urow3 = sht3.range('B2').end('down').last_cell.row
sht3.range("5" + ":" + str(urow3)).clear_contents()
sht3.range('B2').options(pd.DataFrame, index=False).value = shp3

#VINCULACION_SV_NUE
sht4 = wb.sheets['VINCULACION_SV_NUE'].activate()
sht4 = wb.sheets['VINCULACION_SV_NUE']
in_features4 = str(Path(__file__).parent / Path("INSUMOS/VV.shp"))
shp4 = gpd.GeoDataFrame.from_file(in_features4, ignore_geometry=True, index=False)
shp4 = shp4[['INTERNO_SE', 'TIPO_SENAL', 'FASE_ACCIO', 'CONTENIDO1', 'CONTENIDO2', 'VELOCIDAD']]
shp4.insert(2, "CIV", '', allow_duplicates=False)

urow4 = sht4.range('B2').end('down').last_cell.row
sht4.range("5" + ":" + str(urow4)).clear_contents()
sht4.range('B2').options(pd.DataFrame, index=False).value = shp4

#VINCULACION_SV_ACT
sht5 = wb.sheets['VINCULACION_SV_ACT'].activate()
sht5 = wb.sheets['VINCULACION_SV_ACT']
in_features5 = str(Path(__file__).parent / Path("INSUMOS/VV_ACT.shp"))
shp5 = gpd.GeoDataFrame.from_file(in_features5, ignore_geometry=True, index=False)
shp5 = shp5[['INTERNO_SE', 'TIPO_SENAL', 'FASE_ACCIO']]

urow5 = sht5.range('C2').end('down').last_cell.row
sht5.range("5" + ":" + str(urow5)).clear_contents()
sht5.range('C2').options(pd.DataFrame, index=False).value = shp5

#VINCULACION_SV_CIV
sht6 = wb.sheets['VINCULACION_SV_CIV'].activate()
sht6 = wb.sheets['VINCULACION_SV_CIV']
in_features6 = str(Path(__file__).parent / Path("INSUMOS/VV_CIV.shp"))
shp6 = gpd.GeoDataFrame.from_file(in_features6, ignore_geometry=True, index=False)
shp6 = shp6[['INTERNO_SE', 'TIPO_SENAL', 'MVICIV']]

urow6 = sht6.range('B2').end('down').last_cell.row
sht6.range("5" + ":" + str(urow6)).clear_contents()
sht6.range('B2').options(pd.DataFrame, index=False).value = shp6

wb.save(in_file)