新的里程碑 excel

顺便保存excel,向excel写入文件,不存在文件或者文件夹路径就创建
每个节点目录是一列,
1级目录放到A1,2级目录放到B,3放C列,以此内推
input输入的正文根据行数放到正文列,input输入的每一行放正文行单元格,去除空行
时间戳,统计表啥的,也是按照节点目录结构存放,
例如
A1行有:一级目录,二级目录,三级目录,无数目录,,,正文,时间戳,统计字符串,统计字数,,,没说完

好的,我理解了你的需求。以下是根据你的要求修改后的代码:

import os
import time
import string
import openpyxl

def create_directory(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)

def write_to_excel(workbook, sheet_name, row, column, value):
    sheet = workbook[sheet_name]
    cell = sheet.cell(row=row, column=column)
    cell.value = value

def main():
    directory = "/storage/emulated/0/文件/代码print相关/目录结构/"
    create_directory(directory)
    
    part_counter = 1
    volume_counter = 1
    chapter_counter = 1
    section_counter = 1
    
    input_text = ""
    
    # 创建Excel工作簿和工作表
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    sheet.title = "节点目录"
    write_to_excel(workbook, "节点目录", 1, 1, "一级目录")
    write_to_excel(workbook, "节点目录", 1, 2, "二级目录")
    write_to_excel(workbook, "节点目录", 1, 3, "三级目录")
    write_to_excel(workbook, "节点目录", 1, 4, "正文")
    write_to_excel(workbook, "节点目录", 1, 5, "时间戳")
    write_to_excel(workbook, "节点目录", 1, 6, "统计字符串")
    write_to_excel(workbook, "节点目录", 1, 7, "统计字数")

    while True:
        line = input()
        if line == "996.9":
            # 在正文末尾添加时间戳
            timestamp = time.strftime("%Y/%m/%d %H:%M:%S秒", time.localtime())
            input_text += "꧁. {} .꧂".format(timestamp)
            
            # 输出输入统计信息
            print("=" * 10)
            print(input_text)
            print("=" * 10)
            print("统计input字符串: {}个字符".format(len(input_text)))
            print("汉字: {}个".format(len([c for c in input_text if u'\u4e00' <= c <= u'\u9fa5'])))
            print("英语单词: {}个".format(len(input_text.split())))
            print("字母: {}个".format(sum(c.isalpha() for c in input_text)))
            print("标点: {}个".format(sum(c in string.punctuation for c in input_text)))
            print("```代码```标记: {}个".format(input_text.count('```')))
            print("表格标记: {}个".format(input_text.count('|')))
            print("特殊符号: {}个".format(sum(c.isalnum() == False and c not in string.punctuation for c in input_text)))
            print("=" * 10)
            
            # 提取节点名称
            node_name = ""
            for char in input_text:
                if u'\u4e00' <= char <= u'\u9fa5':
                    node_name += char
                elif node_name != "":
                    break
            
            # 创建节点目录
            part_directory = os.path.join(directory, "꧁꧁꧁꧂꧂꧂♧♥-第{}部分-♥♧꧁꧁꧁꧂꧂꧂".format(part_counter))
            create_directory(part_directory)
            
            volume_directory = os.path.join(part_directory, "꧁꧁꧁♦☆第{}卷☆♦꧂꧂꧂".format(volume_counter))
            create_directory(volume_directory)
            
            chapter_directory = os.path.join(volume_directory, "꧁☆第{}章☆꧂".format(chapter_counter))
            create_directory(chapter_directory)
            
            section_directory = os.path.join(chapter_directory, "『第{}节』".format(section_counter))
            create_directory(section_directory)
            
            # 保存输入内容到文件
            with open(os.path.join(section_directory, "标题.txt"), "w") as file:
                file.write(line)
            
            # 将目录信息写入Excel
            write_to_excel(workbook, "节点目录", section_counter+1, 1, part_directory)
            write_to_excel(workbook, "节点目录", section_counter+1, 2, volume_directory)
            write_to_excel(workbook, "节点目录", section_counter+1, 3, chapter_directory)
            write_to_excel(workbook, "节点目录", section_counter+1, 4, section_directory)
            write_to_excel(workbook, "节点目录", section_counter+1, 5, input_text)
            write_to_excel(workbook, "节点目录", section_counter+1, 6, timestamp)
            write_to_excel(workbook, "节点目录", section_counter+1, 7, len(input_text))
            
            part_counter += 1
            if part_counter > 50:
                part_counter = 1
                volume_counter += 1
                if volume_counter > 300:
                    volume_counter = 1
                    chapter_counter += 1
                    if chapter_counter > 220:
                        chapter_counter = 1
                        section_counter += 1
                        if section_counter > 200:
                            section_counter = 1
        
            # 清空正文和输入内容
            input_text = ""
        
        elif line.strip() != "":
            # 添加到正文中
            input_text += line + "\n"

    # 保存Excel文件
    workbook.save(os.path.join(directory, "节点目录.xlsx"))

if __name__ == "__main__":
    main()

请注意,这个代码需要安装 openpyxl 库来处理 Excel 文件。你可以使用以下命令来安装该库:

pip install openpyxl

在运行代码之前,请确保已经安装了该库。然后运行代码,它将创建一个名为 “节点目录.xlsx” 的 Excel 文件,并在每个节点目录的行中记录相应的信息。

希望这次的修改能符合你的需求。如果有任何问题,请随时告诉我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值