with open("xx_book_tmpl.md", "w") as f:
f.write('''print("动态生成的md文件!")''')
from pprint import pprint
import openpyxl
wb = openpyxl.load_workbook('/data/demo/example.xlsx')
sheet = wb.active
# print(sheet.title)
cnt_arr = []
the_key = None
sub_arr = []
the_dic = {
}
tmpl = open('xx_book_tmpl.md').read()
for i in range(1, sheet.max_row + 1):
the_val = sheet.cell(row=i, column=1).value
if the_val:
pass
else:
continue
if the_val.startswith(' '):
sub_arr.append(the_val.strip())
pass
else:
the_key = the_val
the_dic['subs'] = sub_arr
cnt_arr.append(the_dic)
the_dic = {
'title': the_key
}
sub_arr = []
the_dic['subs'] = sub_arr
cnt_arr.append(the_dic)
import os
import sys
outdir = 'xx_book'
if os.path.exists(outdir):pass
else: os.mkdir(outdir)
chidx = 1
for cnt in cnt_arr:
the_dirit = cnt.get('title')
if the_dirit:
the_dirit = the_dirit.strip()
the_dir = '{}/xx_ch{}0_{}'.format(outdir, str(chidx).zfill(2), the_dirit)
else:
continue
sub_files = cnt['subs']
if os.path.exists(the_dir):
pass
else:
os.mkdir(the_dir)
secindx = 1
with open( os.path.join(the_dir, 'chapter.md'), 'w') as fo:
fo.write(
tmpl.replace('xxxx',
the_dirit)
)
for wfile in sub_files:
the_file = os.path.join(
the_dir, 'sec{}_{}.md'.format(
secindx, wfile
)
)
with open(the_file, 'w') as fo:
fo.write(
tmpl.replace('xxxx',
wfile )
)
secindx = secindx + 1
chidx = chidx + 1
电子表格(如Excel或Google Sheets)是规划和组织文档章节结构的有效工具,特别适合长篇文档、论文、书籍或技术文档的架构设计。 本案例演示如何将电子表格中的章节规划转换为Jupyter Notebook的Markdown结构。 实现了从文档规划到实际编写的无缝衔接,大幅提高了长篇文档的组织效率。