# coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# __author__ = '郭 璞'
# __date__ = '2016/8/5'
# __Desc__ = 自动生成网页相册
import os
# 呵呵了,原来有标准库中的walk方法。那么这个方法就获得一个文件夹下的图片文件吧
def getFiles(filepath):
files = []
if os.path.isdir(filepath):
for file in os.listdir(filepath):
if os.path.isdir(file):
getFiles(file)
elif file.endswith('.jpg') or file.endswith('.png') or file.endswith('.gif'):
files.append(filepath + str(file))
elif os.path.isfile(filepath):
files.append(filepath)
return files
# 获取给定目录下所有以.jpg .png .gif结尾的文件,并补全路径保存到列表中输出
def recourse(filepath):
files = []
for fpathe, dirs, fs in os.walk(filepath):
for f in fs:
if f.endswith('.jpg') or f.endswith('.png') or f.endswith('.gif'):
files.append(os.path.join(fpathe, f))
return files
# 生成网页源码文件,指定
def generate(files, shuffle=False):
template_start = '''
<html><head><meta charset='utf-8'><title>网页版相册</title><link rel="stylesheet" type="text/css" href="csshake-slow.min.css">
<link rel="stylesheet" type="text/css" href="http://csshake.surge.sh/csshake-slow.min.css"></script></head><body>
'''
template_body = ''
# 如果指定乱序,就乱序列表中的数据
if shuffle == True:
from random import shuffle
shuffle(files)
for file in files:
template_body += '<a href="' + file + '"><img class="shake-slow" src="' + file + '" style="width:64px;height:auto;"></a>'
template_end = '''
</body></html>
'''%
html = template_start + template_body + template_end
return html
# 生成html文件,并输出到指定的目录
def write2File(filepath, data):
file = open(filepath, 'wb')
file.write(data)
file.close()
print 'Write to file Scuuess!'
if __name__ == "__main__":
# E:\Picture\LOFTER\
filepath = 'E:\Picture\LOFTER\'
files = recourse(filepath=filepath)
for item in files:
print item
html = generate(files, True)
output_path = r'C:UsersAdministratorDesktoptest.html'
write2File(filepath=output_path, data=html)
print 'HTML相册文件已生成在桌面,请查看'
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有