Python实例:制作图片电子书

来自CloudWiki
跳转至: 导航搜索

遍历文件夹中的图片,并制作图片电子书:

import os
def infectHtml(fileName, infectedContent):
    with open(fileName, 'r') as fp:
        lines = fp.readlines()
    for index, line in enumerate(lines):
        if line.strip().lower().startswith('<body>'):
            lines.insert(index+1, infectedContent)
            break
    with open(fileName, 'w') as fp:
        fp.writelines(lines)


s=input("pls input the dir:")
str1="<p><img src=\""+s+"\\"
str2="\" width=\"600px\"/></p>"
x=[]
for fname in os.listdir(s):
    content=str1+fname+str2
    print(content)
    x.append(content)

while len(x)!=0:
    info =x.pop()
    infectHtml('index.html',info)