Python | 使用Python编写Word文档v1

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_39591494/article/details/83477171
#!/usr/bin/env python
# -*- coding:utf-8 -*-

__Author__ = "YanZanG"
__product__ = "Spiritual conversion"


import PyPDF2
import openpyxl
import os
import sys
from docx import Document
from color_me import ColorMe

class Office(object):
    def __init__(self, file_path, file_name):
        self.file_path = file_path
        self.file_name = file_name
        self.Doc = Document()
        self.index = 0

    def user_input(self):
        os.chdir(self.file_path)
        Count = True
        while (Count):
            Your_add_heading = input("请您输入标题:")
            Your_add_paragraph = input("请您输入内容:")

            if Your_add_heading.strip() == "" or Your_add_paragraph.strip() == "":
                print("标题或者内容不能为空,请您重新输入!!!")
                Count = True
            else:
                self.write(Your_add_heading, Your_add_paragraph)
                Count = False

    def write(self, Your_add_heading, Your_add_paragraph):
        self.index += 1
        self.Doc.add_heading(Your_add_heading, level=self.index)
        self.Doc.add_paragraph(Your_add_paragraph)
        self.carry()

    def carry(self):
        Count = True
        while(Count):
            print("你是否还需要继续添加内容?(yes/no)")
            User_result = input("请您选择:").upper()
            if User_result.strip() == "":
                print("对不起,选择不能为空呀")
            elif User_result.strip() == "YES":
                self.user_input()
            elif User_result.strip() == "NO":
                self.Doc.save(self.file_name + ".docx")
                print("保存并退出成功,请您立即查看,欢迎再次使用,Bye~")
                sys.exit()




def main():
	welcome = ColorMe(f"Welcome to the {__product__} applet".center(60,"_")).green()
	print(welcome)
	Info_error = ColorMe("输入路径例如:F:\python-office").green()
	
    Count = True
    while(Count):
        Your_path = input("请您输入您需要保存的路径:").strip()
        Your_file_name = input("为你的情书起一个名字吧:").strip()
        if os.path.exists(Your_path):
            Success = ColorMe(f"系统检测{Your_path}路径成功!!!").green()
            print(Success)
            office_word = Office(Your_path, Your_file_name)
            office_word.user_input()
            Count = False
        else:
            file_error = ColorMe(f"系统检测'{Your_path}'路径失败,请您输入正确的保存路径").red()
            print(file_error)
            print(Info_error)

if __name__ == '__main__':
    main()

Color_me:

#!/usr/bin/env Python
#-*- coding:utf-8 -*-

__author__ = 'De8ug'

class ColorMe:
    """
    give me color see see...
    实际用起来很简单:
        ColorMe('somestr').blue()
    """
    def __init__(self, some_str):
        self.color_str = some_str

    def blue(self):
        str_list = ["\033[34;1m", self.color_str, "\033[0m"]
        return ''.join(str_list) # "\033[34;1m" + self.color_str + "\033[0m"

    def green(self):
        str_list = ["\033[32;1m", self.color_str, "\033[0m"]
        return ''.join(str_list) # "\033[34;1m" + self.color_str + "\033[0m"

    def yellow(self):
        str_list = ["\033[33;1m", self.color_str, "\033[0m"]
        return ''.join(str_list) # "\033[34;1m" + self.color_str + "\033[0m"

    def red(self):
        str_list = ["\033[31;1m", self.color_str, "\033[0m"]
        return ''.join(str_list) # "\033[34;1m" + self.color_str + "\033[0m"


def main():
    ColorMe('somestr').blue()

if __name__ == '__main__':
    main()

运行结果
在这里插入图片描述
在这里插入图片描述

展开阅读全文

没有更多推荐了,返回首页