目录
- Ⅰ Python基础
- 1. Python入门
- 2. 流程控制
- 3. Python函数使用
- 4. Python中的列表
- 5. 字典
- 6. Python中的字符串
- 7. 异常处理
- Ⅱ Python 进阶
- Ⅲ Python常用类库
- 1. 正则表达式
- 2. 文件读写
- 3. 文件操作
- 4. CSV与JSON
- 5. 日期与时间
- 6. 文本文件
- Ⅳ Python日常工具
- 1. 用 Python 处理电子表格
- 2. DOCX 文档解析与处理
- 3. 用Python进行图像处理
- 4. 使用Python处理PDF文档
- 5. Web信息解析与处理
- 6. Web信息生成与发布
- 7. 自动化Email处理
- 8. 杂项
- Ⅴ 专题
- 1. Pillow图像处理
- 2. OpenCV处理
- 3. 数据库自动化报表处理:使用SQLite
- 4. Python和Web
- 5. Web信息处理
- 6. Web模板引擎
- 7. XML 处理
- Ⅵ 图像处理:使用scikit-image
- 1. skimage数字图像处理基础
- 2. skimage数字图像处理中级
- 3. skimage数字图像处理高级
- Ⅶ 科学计算
- 1. NumPy数值计算
- 2. SciPy科学类库
- 3. SymPy符号运算
- 4. Matplotlib绘图
- 5. Pandas 第一部分
- 6. Pandas 第二部分
- 6.1. Pandas排序
- 6.2. Pandas字符串和文本数据
- lower()函数示例
- upper()函数示例
- len()函数示例
- strip()函数示例
- split(pattern)函数示例
- cat(sep=pattern)函数示例
- get_dummies()函数示例
- contains()函数示例
- replace(a,b)函数示例
- repeat(value)函数示例
- count(pattern)函数示例
- startswith(pattern)函数示例
- endswith(pattern)函数示例
- find(pattern)函数示例
- findall(pattern)函数示例
- swapcase()函数示例
- islower()函数示例
- isupper()函数示例
- isnumeric()函数示例
- 6.3. Pandas选项和自定义
- 6.4. Pandas索引和选择数据
- 6.5. Pandas统计函数
- 6.6. Pandas窗口函数
- 6.7. Pandas聚合
- 6.8. Pandas缺失数据
- 6.9. Pandas分组(GroupBy)
- 6.10. Pandas合并/连接
- 6.11. Pandas级联
- 7. Pandas 第三部分
- Ⅷ 自然语言与知识图谱
上一个主题
下一个主题
1.6. 案例:使用电子表格生成章节结构¶
场景说明¶
根据电子表格内容生成Jupyter章节的结构。
解决思路¶
解决方法¶
>>> from pprint import pprint
>>> import openpyxl
>>> wb = openpyxl.load_workbook('contents.xlsx')
>>>
>>> sheet = wb.active
>>> # print(sheet.title)
>>>
>>> cnt_arr = []
>>> the_key = None
>>>
>>> sub_arr = []
>>> the_dic = {
>>>
>>> }
>>>
>>> tmpl = open('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