Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- from openpyxl import load_workbook
- from openpyxl.drawing.image import Image
- from PIL import Image as PILImage
- import base64
- from io import BytesIO
- # 读取Excel文件
- excel_path = 'path_to_your_excel_file.xlsx'
- workbook = load_workbook(excel_path)
- sheet = workbook.active
- # 提取数据
- data = []
- for row in sheet.iter_rows(min_row=2, values_only=False):
- text = row[0].value
- img1 = row[1].image
- img2 = row[2].image
- data.append((text, img1, img2))
- # 创建HTML文件
- html_content = '''
- <html>
- <head>
- <title>Excel to HTML</title>
- </head>
- <body>
- <table border="1">
- <tr>
- <th>Text</th>
- <th>Image 1</th>
- <th>Image 2</th>
- </tr>
- '''
- for text, img1, img2 in data:
- html_content += '<tr>'
- html_content += f'<td>{text}</td>'
- for img in [img1, img2]:
- # Convert the image to base64
- if img is not None:
- img_buffer = BytesIO()
- img.image.save(img_buffer, format='PNG')
- img_str = base64.b64encode(img_buffer.getvalue()).decode('utf-8')
- html_content += f'<td><img src="data:image/png;base64,{img_str}" alt="Image"></td>'
- else:
- html_content += '<td>No Image</td>'
- html_content += '</tr>'
- html_content += '''
- </table>
- </body>
- </html>
- '''
- # 保存HTML文件
- with open('output.html', 'w', encoding='utf-8') as f:
- f.write(html_content)
- print("HTML file created successfully.")
Add Comment
Please, Sign In to add comment