Files
openmlsys-zh/tools/format_tables.py
2022-03-20 10:12:27 +08:00

41 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from bs4 import BeautifulSoup
import os
"""
1. 本脚本文件会设置所有html文件使得表格居中显示。
2. 确保你已经安装了bs4, 具体可以通过pip install bs4 进行安装
"""
root_path = "./"
html_root_path = "_build/html/"
def get_html_list():
index_html_path = os.path.join(root_path, html_root_path, "index.html")
index_soup = BeautifulSoup(open(index_html_path))
content_list = index_soup.find(name="div", attrs={"class": "globaltoc"}). \
find_all(name="a", attrs={"class": "reference internal"})
html_list = [os.path.join(html_root_path, content_name["href"]) for content_name in content_list]
return html_list
def format_table():
html_list = get_html_list()
for html_file in html_list:
try:
soup = BeautifulSoup(open(html_file))
all_tables = soup.find_all(name="table", attrs={"class": "docutils align-default"})
for table in all_tables:
table["style"] = "margin-left:auto;margin-right:auto;margin-top:10px;margin-bottom:20px;"
if len(all_tables):
write_out_file = open(html_file, mode="w")
write_out_file.write(soup.prettify())
write_out_file.close()
except:
pass
if __name__ == "__main__":
format_table()