【问题标题】:How to solve error when querying the .shp file to Excel?将 .shp 文件查询到 Excel 时如何解决错误?
【发布时间】:2021-08-31 19:07:43
【问题描述】:

我正在使用 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\3.9\00020813-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)

更新:到目前为止我尝试过并且有效的方法是将shp和sht的每个过程的代码分开,即我在不同的. py 选择结果将去哪里的 excel 工作表,以这种方式它做得很好,我正在尝试向 python 代码添加一条指令,在每个 # ... 选择要处理的 excel 工作表并继续直到保存。

【问题讨论】:

  • Book.save(path) 是“另存为...”;可能是使用未更改的已打开文件名不起作用。只需尝试不带参数的 wb.save()。
  • @Redoute 尝试 wb.save () 并给出错误并意外关闭了对我有用的 excel 文件,将其作为更新放入问题正文中。

标签: python excel pandas dataframe xlwings


【解决方案1】:

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

我分享结果和功能代码,希望有人会发现这篇文章有用

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)

【讨论】:

    猜你喜欢
    • 2011-10-05
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多