zsh2517

Untitled

Jun 13th, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import pandas as pd
  2. from openpyxl import load_workbook
  3. from openpyxl.drawing.image import Image
  4. from PIL import Image as PILImage
  5. import base64
  6. from io import BytesIO
  7.  
  8. # 读取Excel文件
  9. excel_path = 'path_to_your_excel_file.xlsx'
  10. workbook = load_workbook(excel_path)
  11. sheet = workbook.active
  12.  
  13. # 提取数据
  14. data = []
  15. for row in sheet.iter_rows(min_row=2, values_only=False):
  16. text = row[0].value
  17. img1 = row[1].image
  18. img2 = row[2].image
  19.  
  20. data.append((text, img1, img2))
  21.  
  22. # 创建HTML文件
  23. html_content = '''
  24. <html>
  25. <head>
  26. <title>Excel to HTML</title>
  27. </head>
  28. <body>
  29. <table border="1">
  30. <tr>
  31. <th>Text</th>
  32. <th>Image 1</th>
  33. <th>Image 2</th>
  34. </tr>
  35. '''
  36.  
  37. for text, img1, img2 in data:
  38. html_content += '<tr>'
  39. html_content += f'<td>{text}</td>'
  40.  
  41. for img in [img1, img2]:
  42. # Convert the image to base64
  43. if img is not None:
  44. img_buffer = BytesIO()
  45. img.image.save(img_buffer, format='PNG')
  46. img_str = base64.b64encode(img_buffer.getvalue()).decode('utf-8')
  47. html_content += f'<td><img src="data:image/png;base64,{img_str}" alt="Image"></td>'
  48. else:
  49. html_content += '<td>No Image</td>'
  50.  
  51. html_content += '</tr>'
  52.  
  53. html_content += '''
  54. </table>
  55. </body>
  56. </html>
  57. '''
  58.  
  59. # 保存HTML文件
  60. with open('output.html', 'w', encoding='utf-8') as f:
  61. f.write(html_content)
  62.  
  63. print("HTML file created successfully.")
Add Comment
Please, Sign In to add comment