python读取excel和读取excel图⽚总结
1.读取Excel表格
panda⽅式 ,普通的excel可⾏,对于有些数据复杂的,可能提取不了
import pandas
ad_excel('现代征信学.xlsx',index_col='列名',header=None)          #header默认为0,指读取第⼏⾏,0代表第⼀⾏
print(excel)
xlrd⽅式
#读取excel表格
def readExcelTable(file_path,sheetName):
table = xlrd.open_workbook(file_path)
#sheet=table.sheet_by_index(0)# ⽤下标的⽅式选择要读取⽂件中的⼯作表,也可⽤⼯作表的名称 sheet=work.sheet_by_name('Sheet1')
sheet = table.sheet_by_name(sheetName)
print(sheet)
print(ws))
# 遍历excel,打印所有数据
for i in ws):
if i>0:
w_values(i)
numId='0'
if item[0]!=None and item[0]!='':
numId=str(int(item[0]))
jItem = {'Id':numId,'Pic': str(item[2])+'.png', 'Code':str(item[2]), 'TypeName': str(item[3]), 'Oe': str(item[4]), 'Brand': str(item[5]),'Car':'','Size':str(item[7]),'Package':str(item[8]),'Memo':str(item[9])};
print(jItem)
MSSQL().insertProduct(jItem)
2.python读取excel图⽚
原理都是通过rar模式获取excel的资源⽂件,如:
将“奥圣2021-04-10((1).xls” 重命名为“奥圣2021-04-10((1).rar”
图⽚⽬录都在\media下
图⽚和excel⾏的对应关系是 l,总的来说如果这个excel⽂件的数据不是那么规范的话,处理还是有点⿇烦的
python怎么读入excel⽤IE浏览器打开l
那么接下来的问题就是读取这个xml,并到对应关系,进⾏相应的重命名或存储了
#excel变成压缩包后,图⽚是在media⽬录下⾯,但⽂件名是顺序递增的序号
#其中 <a:blip r:embed="rId52"/> 节点对应图⽚序号
#<xdr:cNvPr descr="机滤AO62121" name="Picture 251" id="3784784"/> 对应图⽚描述
#<xdr:row>141</xdr:row> 对应excel⾏号
def zipXmlRead(zipfile_path,image_path):
dir_path = os.path.dirname(zipfile_path)  # 获取⽂件所在⽬录
file_name = os.path.basename(zipfile_path)  # 获取⽂件名
unzip_dir = os.path.join(dir_path, str(file_name.split('.')[0]))
xml_name = 'xl' + os.sep + 'drawing1'+os.sep+'l'  # excel变成压缩包后,再解压,drawing1对应就是sheet1的说明
xml_name = 'xl/l'
ns = {'i': '/drawingml/2006/spreadsheetDrawing','a':'/drawingml/2006/main','r':'/drawingml/2006/relationships'}    fz = zipfile.ZipFile(zipfile_path, 'r')
xml_string = fz.read(xml_name).decode()
xml = ET.fromstring(xml_string)
nodes = xml.findall('.//i:from/i:row', ns) #test:⾏号
for node in nodes:
#print(node) 测试
continue
nodes=xml.findall(".//i:pic/i:blipFill/a:blip",ns) #test:图⽚节点
for node in nodes:
#print(node.attrib) 测试
continue
nodes=xml.findall('.//i:twoCellAnchor',ns) #到⽗节点,再遍历⼦节点
for node in nodes:
row=node.find('.//i:from/i:row', ns)#获取⾏号
descr=''
descrNode=node.find('.//i:nvPicPr/i:cNvPr',ns) #获取描述
if 'descr' in descrNode.attrib:
descr=descrNode.attrib['descr']
rid=node.find('.//i:blipFill/a:blip',ns).attrib['{/officeDocument/2006/relationships}embed'] #获取图⽚资源序号
print('⾏号:'+str(rowNum)+' 描述:'+descr+' 图⽚顺序:'+rid)
imgId=str(rid).replace('rId','') #
proCode=MSSQL().getCodeById(int(rowNum))
picName=rowNum+'.png'
newPicName=str(proCode)
try:
except IOError as e:
print(e)