Brak opisu

testpd_xlsx.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import pandas as pd
  2. # Read HTML content (assuming it contains a table)
  3. html_content = """
  4. <html>
  5. <head>
  6. <style>
  7. table {
  8. margin-left: 20px; /* Add margin to the left */
  9. border-collapse: collapse; /* Ensure proper border styling */
  10. }
  11. th, td {
  12. border: 1px solid black;
  13. padding: 8px;
  14. text-align: center;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <table border="1" class="dataframe">
  20. <thead>
  21. <tr style="text-align: center;">
  22. <th>Column1</th>
  23. <th>Column2</th>
  24. <th>Column3</th>
  25. <!-- Add more columns based on your DataFrame -->
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <tr>
  30. <td>Value1</td>
  31. <td>Value2</td>
  32. <td>Value3</td>
  33. <!-- Add rows dynamically based on your DataFrame -->
  34. </tr>
  35. <tr>
  36. <td>ValueA</td>
  37. <td>ValueB</td>
  38. <td>ValueC</td>
  39. </tr>
  40. </tbody>
  41. </table>
  42. </body>
  43. </html>
  44. """
  45. # Parse HTML table into a DataFrame
  46. dfs = pd.read_html(html_content)
  47. df = dfs[0] # Assuming the first table is the one we need
  48. # Save DataFrame to Excel
  49. output_path = "output.xlsx"
  50. df.to_excel(output_path, index=False)
  51. print(f"File saved as {output_path}")