从Excel到表转换时出现错误

问题描述 投票:0回答:2

我刚刚开始学习Python,现在我正尝试将其与GIS知识相集成。如标题所示,我试图将Excel工作表转换为表格,但我不断收到错误,一个错误对我来说是完全无法理解的,另一个错误似乎表明我的文件不存在,我知道这是不正确的因为我直接从属性复制了它的位置。

这里是我的环境的屏幕截图。如果可以的话请帮助,并提前致谢。

Environment/Error

arcgis arcpy
2个回答
0
投票

简单设置,您将工作空间目录放在filename变量内,因此当arcpy处理它时,它将尝试访问未知工作空间中不存在的文件。

尝试一下。

arcpy.env.workspace = "J:\egis_work\dpcd\projects\SHARITA\Python\"
arcpy.ExcelToTable_conversion("Exceltest.xlsx", "Bookstorestable", "Sheet1")

0
投票

Arcpy使用以下语法转换geodatabase tables to excelenter image description here

很简单。

示例地理数据库表位置:

enter image description here

说我想将上表转换为根文件夹(为什么要使用根文件夹?因为.xls或.xlsx无法存储在地理数据库中,所以我将按照以下步骤进行:]]

import arcpy
import os
from datetime import datetime, date, time

# Set environment settings
in_table= r"C:\working\Sunderwood\Network Analyst\MarchDistances\Centroid.gdb\SunderwoodFirstArcpyTable"

#os.path.basename(in_table)

out_xls= os.path.basename(in_table)+ datetime.now().strftime('%Y%m%d') # Here

#os.path.basename(in_table)- Gives the base name of pathname. In this case, it returns  the name table
# + is used in python to concatenate
# datetime.now()- gives todays date
# Converts todays date into a string in the format YYYMMDD
# Please add all the above statements and you notice you have a new file name which is the table you input plus todays date


#os.path.dirname() method in Python is used to get the directory name from the specified path
geodatabase = os.path.dirname(in_table)
# In this case, os.path.dirname(in_table)  gives us the geodatabase


# The The join() method takes all items in an iterable and joins them into one string
SaveInFolder= "\\".join(geodatabase.split('\\')[:-1])

# This case, I tell python take \ and join on the primary directory above which I have called geodatabase. However, I tell it to remove some characters. I will explain the split below.

# I use split method. The split() method splits a string into a list
#In the case above it splits into  ['W:\\working\\Sunderwood\\Network', 'Analyst\\MarchDistances\\Centroid.gdb']. However, that is not what I want. I want to remove "\\Centroid.gdb" so that I remain with the follwoing path ['W:\\working\\Sunderwood\\Network', 'Analyst\\MarchDistances']

#Before I tell arcpy to save, I have to specify the workspace in which it will save. So I now make my environment the SaveInFolder
arcpy.env.workspace =SaveInFolder

## Now I have to tell arcpy what I will call my newtable. I use os.path.join.This method concatenates various path components with exactly one directory separator (‘/’) following each non-empty part except the last path component
newtable = os.path.join(arcpy.env.workspace, out_xls)

#In the above case it will give me "W:\working\Sunderwood\Network Analyst\MarchDistances\SunderwoodFirstArcpyTable20200402"

# You notice the newtable does not have an excel extension. I resort to + to concatenate .xls onto my path and make it "W:\working\Sunderwood\Network Analyst\MarchDistances\SunderwoodFirstArcpyTable20200402.xls"

table= newtable+".xls"

#Finally, I call the arcpy method and feed it with the required variables
# Execute TableToExcel
arcpy.TableToExcel_conversion(in_table, table)
© www.soinside.com 2019 - 2024. All rights reserved.