Output the contents of ~ .xlsx in the folder to HTML with Python

Thing you want to do

When you have a lot of files created in Excel in a folder and you don't know which one, don't you feel like "it's tedious to look up one by one"? Therefore, only the head of all the ~ .xlsx files in the folder will be output as HTML. やりたいこと.png

This should save you some effort knowing which file you are looking for ...

Library to use, etc.

I'm running Python 3.8 on Windows 10.

os
Used for moving folders
glob
Used to inspect files in folders
io
Used for writing to files
pandas
.xlsx reading / HTML conversion
webbrowser
Used to open the output HTML in a browser

Source code

The source code is as follows.

python


import glob
import io
import os
import webbrowser
import pandas as pd

folder = input('Please enter the folder path\n')
os.chdir(folder)

files_in_folder = [i.lstrip('.\\') for i in glob.glob("./*")]
xlsx_in_folder = [i for i in files_in_folder if i.endswith('.xlsx')]  # .xlsx leave only the end

with io.StringIO() as s:
    s.write('<!DOCTYPE html>\n<html lang="jp">\n<head>\n\t<meta '
            'charset="UTF-8">\n\t<title>.xlsx summary</title>\n</head>\n<body>\n')
    s.write('<h1>.xlsx summary</h1>\n')

    # .Read xlsx and output html with 5 rows and 3 columns--from here
    for i in xlsx_in_folder:
        s.write('<br>\n')
        s.write(i)  #file name
        s.write(pd.read_excel(i, header=None, usecols=[0, 1, 2]).head().to_html(header=None, index=None))
    # .Read xlsx and output html with 5 rows and 3 columns--So far

    s.write('</body>\n</html>')
    output = s.getvalue()

with open("output.html", mode='w', encoding='utf-8') as f:
    f.write(output)

webbrowser.open("output.html")

output

HTML is output like this and opened in the default browser. memo.png

Recommended Posts

Output the contents of ~ .xlsx in the folder to HTML with Python
I want to output the beginning of the next month with Python
Output the number of CPU cores in Python
[Note] Export the html of the site with python.
How to get the files in the [Python] folder
Output in the form of a python array
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to get the number of digits in Python
[python] option to turn off the output of click.progressbar
Convert the image in .zip to PDF with Python
I tried to open the latest data of the Excel file managed by date in the folder with Python
Not being aware of the contents of the data in python
Try to get the contents of Word with Golang
[Python] How to output the list values in order
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
How to get a list of files in the same directory with python
Run the output code with tkinter, saying "A, pretending to be B" in python
How to identify the element with the smallest number of characters in a Python list?
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
Output the specified table of Oracle database in Python to Excel for each file
How to count the number of occurrences of each element in the list in Python with weight
It is easy to execute SQL with Python and output the result in Excel
I tried to find the entropy of the image with python
Try scraping the data of COVID-19 in Tokyo with Python
Change the standard output destination to a file in Python
Calculate the square root of 2 in millions of digits with python
Settings to debug the contents of the library with VS Code
[Homology] Count the number of holes in data with Python
Try to automate the operation of network devices with Python
Batch convert all xlsx files in the folder to CSV files
Get the source of the page to load infinitely with python.
Output to csv file with Python
In the python command python points to python3.8
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
Return the image data with Flask of Python and draw it to the canvas element of HTML
How to check if the contents of the dictionary are the same in Python by hash value
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
Process the contents of the file in order with a shell script
How to determine the existence of a selenium element in Python
How to know the internal structure of an object in Python
[python, ruby] fetch the contents of a web page with selenium-webdriver
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
How to check the memory size of a variable in Python
Read the standard output of a subprocess line by line in Python
Feel free to change the label of the legend in Seaborn in python
I wrote the code to write the code of Brainf * ck in python
How to check the memory size of a dictionary in Python
[TensorFlow 2] How to check the contents of Tensor in graph mode
Visualize the frequency of word occurrences in sentences with Word Cloud. [Python]
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
Create a function to get the contents of the database in Go
I tried to improve the efficiency of daily work with Python
Get a list of files in a folder with python without a path
[Python] A program that rotates the contents of the list to the left
PhytoMine-I tried to get the genetic information of plants with Python
Try logging in to qiita with Python
Check the behavior of destructor in Python
HTML email with image to send with python
Summary of character string format in Python3 Whether to live with the old model or the new model