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.
This should save you some effort knowing which file you are looking for ...
I'm running Python 3.8 on Windows 10.
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")
HTML is output like this and opened in the default browser.
Recommended Posts