gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
用python生成一个Html文件,生成步骤如下:1)读取c:\tmp目录下所有jpg文件,这些文件的文件名都具有“”姓名-IP-时间戳.jpg”的格式,读取图片文件后,生成一个…
- User
用python生成一个Html文件,生成步骤如下:1)读取c:\tmp目录下所有jpg文件,这些文件的文件名都具有“”姓名-IP-时间戳.jpg”的格式,读取图片文件后,生成一个数据表,存放对应的文件名、姓名、IP、时间戳。2)开始HTML文件生成,根据数据表中信息,生成HTML的表格部分,第一列为序号,第二列为图片的缩略图,第三列是姓名,第四列是时间戳,第五列是IP
- Assistant
以下是使用Python生成一个满足要求的HTML文件的示例代码:
import os from PIL import Image # 1)读取c:\tmp目录下所有jpg文件,生成数据表 image_dir = 'C:\\tmp' data = [] for file_name in os.listdir(image_dir): if file_name.endswith('.jpg'): full_path = os.path.join(image_dir, file_name) name, ip, timestamp = file_name.split('-') thumbnail = Image.open(full_path).thumbnail((100, 100)) data.append({ 'file_name': file_name, 'name': name, 'ip': ip, 'timestamp': timestamp }) # 2)开始HTML文件生成 html_content = ''' <!DOCTYPE html> <html> <head> <style> table {{ border-collapse: collapse; }} th, td {{ border: 1px solid black; padding: 8px; text-align: center; }} img {{ max-width: 100px; max-height: 100px; display: block; margin-left: auto; margin-right: auto; }} </style> </head> <body> <table> <tr> <th>序号</th> <th>图片缩略图</th> <th>姓名</th> <th>时间戳</th> <th>IP</th> </tr> {table_rows} </table> </body> </html> ''' table_rows = '' for i, entry in enumerate(data): table_rows += f''' <tr> <td>{i+1}</td> <td><img src="{os.path.join(image_dir, entry["file_name"])}" alt="thumbnail"></td> <td>{entry["name"]}</td> <td>{entry["timestamp"]}</td> <td>{entry["ip"]}</td> </tr> ''' html_content = html_content.format(table_rows=table_rows) with open('output.html', 'w') as file: file.write(html_content)