python: 读取文件夹下的所有svg文件生成html以便预览

import os

def getImages(filePath):
  result = {}
  for filepath,dirnames,filenames in os.walk(filePath):
    key = filepath.replace('.', '')
    if key not in result.keys():
      result[key] = []
    for filename in filenames:
      image_path = os.path.join(filepath,filename)
      result[key].append({
        'src': image_path,
        'name': filename
      })
  return result

def write(images):
  f = open("./image-view.html", "w+")
  f.write('<html lang="en">\n' + 
    '<head>\n' + 
      '<meta charset="UTF-8">\n' + 
      '<meta http-equiv="X-UA-Compatible" content="IE=edge">\n' + 
      '<meta name="viewport" content="width=device-width, initial-scale=1.0">\n' + 
      '<title>Document</title>\n' + 
      '<style> body > div { margin: 0 40px 40px 40px; padding:40px; display: flex; flex-wrap:wrap; box-shadow: 0 0 20px rgb(0 0 0 / 30%); } ' + 
            'h2.title { padding: 40px 0 20px 40px; font-size: 20px; font-weight: bold; text-align: center; } ' + 
            '.img-block span {     display: inline-block; vertical-align: top;width: 100px;word-break: break-all; margin-right: 24px; }' +  
            '.img-block img { display: inline-block; width: 50px; vertical-align: top; padding: 16px; background: rgba(0,0,0,0.1);margin-right: 16px; margin-bottom: 16px;} ' +
            '.img-block { }' +  
      '</style>\n' + 
    '</head> \n' + 
    '<body>\n')
  for key in images:
    f.write('<h2 class="title"> ' + key + '</h2>\n')
    f.write('<div> \n')
    for item in images[key]:
      f.write('<div class="img-block">')
      f.write(' <img src="' + item['src'] + '" />\n' )
      f.write(' <span>' + item['name'] + ' </span>\n')
      f.write('</div>')
    f.write('</div>\n')
  f.write('</body></html>')
  f.close()

if __name__ == 'main':
  rootdir = '/Users/chencyn/images/'
  write(getImages(rootdir))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值