100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python提取excel中的表格并保存图片

python提取excel中的表格并保存图片

时间:2020-10-25 17:20:26

相关推荐

python提取excel中的表格并保存图片

直接上代码:

__author__ = 'yyzhang'# 只能针对xlsx格式import osimport timefrom multiprocessing import Poolimport xlrdfrom PIL import ImageGrabimport xlwings as xwsave_pic = r"D:\img"os.makedirs(save_pic, exist_ok=True)def excel_catch_screen1(shot_excel,sh_num):try:try:try:app = xw.App(visible=True, add_book=False) # 使用xlwings的app启动wb = app.books.open(shot_excel) # 打开文件except Exception as e:return 0try:sheet = wb.sheets[sh_num] # 选定sheetall = sheet.used_range # 获取有内容的rangeprint(all.value)all.api.CopyPicture() # 复制图片区域sheet.api.Paste() # 粘贴img_name = (os.path.basename(shot_excel)).replace("xlsx", "png")pic = sheet.pictures[0] # 当前图片print(type(pic))# print(pic)pic.api.Copy() # 复制图片img = ImageGrab.grabclipboard() # 获取剪贴板的图片数据print(type(img))pic_name=os.path.splitext(img_name)[0]+"_{}".format(i)+".png"# pic_name = img_name.replace("表格", "{}".format(sh_num))img.save(os.path.join(save_pic, pic_name)) # 保存图片pic.delete() # 删除sheet上的图片except Exception as e:wb.close() # 不保存,直接关闭app.quit()print(i)print(e)return 0wb.close() # 不保存,直接关闭app.quit()except Exception as e:pass# wb.close() # 不保存,直接关闭# app.quit()except Exception as e:print(e)wb.close() # 不保存,直接关闭app.quit()if __name__ == '__main__':pathnames = r"D:\excel"file_list=os.listdir(pathnames)file_list=[i for i in file_list if i.endswith("xlsx") and not i.startswith("~$")]for file in file_list:pathname=os.path.join(pathnames,file)print(pathname)# 获取到文件的sheet数try:b = xlrd.open_workbook(pathname)except Exception as e:continueprint(b)shnum = len(b.sheets())for i in range(shnum):excel_catch_screen1(pathname, i)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。