【无标题】

txt文本生成工具 - 用于dlmstest工具

#!/usr/bin/env python
# -*-  coding: utf-8 -*-
# @Time      : 2023/08/31 14:02
# @Author    : Piu
# @Version   : python3.8
# @File      : pageview230831.py
# @Software  : PyCharm


import codecs
import datetime
import os
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QStackedWidget, \
    QLabel, QLineEdit, QMessageBox, QFileDialog
from dateutil.relativedelta import relativedelta
import iconpic


def utf8toansi(file_path_ori, file_path_obj):
    file_ori = codecs.open(file_path_ori, 'r', 'utf-8')
    file_ori_read = file_ori.read()
    file_obj = codecs.open(file_path_obj, 'w', 'ansi')
    file_obj.write(file_ori_read)


#  get now time to hex string
# %w表示时,1-6表示周一到周六,0表示周日
# print(date.strftime("%w"))
# isoweekday()基于ISO 9601标准的星期几,其中星期一到星期七为1~7
def now_time_tohex_string_isoweekday(nowtimestring):
    numweek = nowtimestring.isoweekday()
    now_time_pattern = nowtimestring.strftime('%Y-%m-%d-%w-%H-%M-%S')
    now_time_pattern_list = now_time_pattern.split('-')
    # year to hex
    # 年高字节中无空格
    now_time_pattern_list[0] = (
            "%02X%02X" % (int(now_time_pattern_list[0]) // 256, int(now_time_pattern_list[0]) % 256))
    # month,day,hour,minute,second to hex
    for index in range(1, len(now_time_pattern_list)):
        now_time_pattern_list[index] = ("%02X" % (int(now_time_pattern_list[index]) % 256))
    numweektohex = ("%02X" % (int(numweek) % 256))
    now_time_pattern_list[3] = numweektohex
    timestringhex = ''
    # year,month,day,
    for timestr in now_time_pattern_list:
        # 每个字符中不添加空格
        timestringhex += timestr
    timestringhex = timestringhex.rstrip()
    return timestringhex


#  get now time to hex string
# %w表示时,1-6表示周一到周六,0表示周日
# print(date.strftime("%w"))
# weekday()其中星期一到星期七为0~6
def now_time_tohex_string_weekday(nowtimestring):
    numweek = nowtimestring.weekday()
    now_time_pattern = nowtimestring.strftime('%Y-%m-%d-%w-%H-%M-%S')
    now_time_pattern_list = now_time_pattern.split('-')
    # year to hex
    # 年高字节中无空格
    now_time_pattern_list[0] = (
            "%02X%02X" % (int(now_time_pattern_list[0]) // 256, int(now_time_pattern_list[0]) % 256))
    # month,day,hour,minute,second to hex
    for index in range(1, len(now_time_pattern_list)):
        now_time_pattern_list[index] = ("%02X" % (int(now_time_pattern_list[index]) % 256))
    numweektohex = ("%02X" % (int(numweek) % 256))
    now_time_pattern_list[3] = numweektohex
    timestringhex = ''
    # year,month,day,
    for timestr in now_time_pattern_list:
        # 每个字符中不添加空格
        timestringhex += timestr
    timestringhex = timestringhex.rstrip()
    return timestringhex


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # 设置标题
        self.setWindowTitle("文本生成")
        # 设置窗口的固定大小
        self.setFixedSize(255, 260)

        # 设置窗口的ICO
        # self.setWindowIcon(QIcon("E:\pyworkcode\dailydo\txtGenTool\emoji.ico"))
        self.setWindowIcon(QIcon(':/emoji.ico'))

        # 创建主窗口的布局
        main_layout = QVBoxLayout()

        # 创建按钮布局
        button_layout = QHBoxLayout()

        # 创建导航栏按钮
        button1 = QPushButton("负荷曲线")
        button2 = QPushButton("日冻结")
        button3 = QPushButton("月结算")

        # 将按钮添加到按钮布局中
        button_layout.addWidget(button1)
        button_layout.addWidget(button2)
        button_layout.addWidget(button3)

        # 创建一个 stacked widget 来容纳页面
        stacked_widget = QStackedWidget()

        # 创建三个页面
        page1 = QWidget()
        page2 = QWidget()
        page3 = QWidget()

        # 设置页面的背景颜色以示区分
        # page1.setStyleSheet("background-color: cornsilk;")
        # page2.setStyleSheet("background-color: limegreen;")
        # page3.setStyleSheet("background-color: lightgreen;")

        # 第一个页面内容 - 负荷曲线
        # 添加串口检测功能到第一个页面
        # 创建第一个输入框和标签
        self.input1_label = QLabel("负荷曲线条数:")
        self.input1_edit = QLineEdit()

        # 创建第二个输入框和标签
        self.input2_label = QLabel("负荷曲线间隔:")
        self.input2_edit = QLineEdit()

        # 创建第三个输入框和标签
        self.input3_label = QLabel("设置起始时间:")
        self.input3_edit = QLineEdit()

        default_value = "2022-06-01 07:59:57"
        self.input3_edit.setText(default_value)

        # 创建第四个输入框和标签
        self.input4_label = QLabel("设置w_week:")
        self.input4_edit = QLineEdit()
        # 添加描述文本到输入框
        self.input4_edit.setPlaceholderText('周格式:0->00-06,1->01-07')

        # 创建文件保存选择按钮
        self.file_button = QPushButton("选择文件保存路径", clicked=self.confirm_loadprofile)

        # 创建确认按钮
        self.confirm_button = QPushButton("生成负荷曲线文本", clicked=self.confirm_loadprofile)

        page1_layout = QVBoxLayout()

        # 将输入框和标签添加到布局中
        page1_layout.addWidget(self.input1_label)
        page1_layout.addWidget(self.input1_edit)

        # 将输入框和标签添加到布局中
        page1_layout.addWidget(self.input2_label)
        page1_layout.addWidget(self.input2_edit)

        # 将输入框和标签添加到布局中
        page1_layout.addWidget(self.input3_label)
        page1_layout.addWidget(self.input3_edit)

        # 将输入框和标签添加到布局中
        page1_layout.addWidget(self.input4_label)
        page1_layout.addWidget(self.input4_edit)

        # # 将确认按钮添加到布局中
        page1_layout.addWidget(self.confirm_button)

        page1.setLayout(page1_layout)

        # 第二个页面内容 - 日冻结
        # 添加串口检测功能到第二个页面
        # 创建第一个输入框和标签
        self.input1_label2 = QLabel("日冻结条数:")
        self.input1_edit2 = QLineEdit()

        # 创建第三个输入框和标签
        self.input3_label2 = QLabel("设置起始时间:")
        self.input3_edit2 = QLineEdit()

        default_value = "2022-06-01 07:59:57"
        self.input3_edit2.setText(default_value)

        # 创建第四个输入框和标签
        self.input4_label2 = QLabel("设置w_week:")
        self.input4_edit2 = QLineEdit()

        # 添加描述文本到输入框
        self.input4_edit2.setPlaceholderText('周格式:0->00-06,1->01-07')

        # 创建文件保存选择按钮
        self.file_button2 = QPushButton("选择文件保存路径", clicked=self.confirm_daily)

        # 创建确认按钮
        self.confirm_button2 = QPushButton("生成日冻结文本", clicked=self.confirm_daily)

        page2_layout = QVBoxLayout()

        # 将输入框和标签添加到布局中
        page2_layout.addWidget(self.input1_label2)
        page2_layout.addWidget(self.input1_edit2)

        # 将输入框和标签添加到布局中
        page2_layout.addWidget(self.input3_label2)
        page2_layout.addWidget(self.input3_edit2)

        # 将输入框和标签添加到布局中
        page2_layout.addWidget(self.input4_label2)
        page2_layout.addWidget(self.input4_edit2)

        # # 将确认按钮添加到布局中
        page2_layout.addWidget(self.confirm_button2)

        page2.setLayout(page2_layout)

        # 第三个页面内容 - 月结算
        # 添加串口检测功能到第三个页面
        # 创建第一个输入框和标签
        self.input1_label3 = QLabel("月结算条数:")
        self.input1_edit3 = QLineEdit()

        # 创建第三个输入框和标签
        self.input3_label3 = QLabel("设置起始时间:")
        self.input3_edit3 = QLineEdit()

        default_value = "2022-06-01 07:59:57"
        self.input3_edit3.setText(default_value)

        # 创建第四个输入框和标签
        self.input4_label3 = QLabel("设置w_week:")
        self.input4_edit3 = QLineEdit()

        # 添加描述文本到输入框
        self.input4_edit3.setPlaceholderText('周格式:0->00-06,1->01-07')

        # 创建文件保存选择按钮
        self.file_button3 = QPushButton("选择文件保存路径", clicked=self.confirm_billing)

        # 创建确认按钮
        self.confirm_button3 = QPushButton("生成月结算文本", clicked=self.confirm_billing)

        page3_layout = QVBoxLayout()

        # 将输入框和标签添加到布局中
        page3_layout.addWidget(self.input1_label3)
        page3_layout.addWidget(self.input1_edit3)

        # 将输入框和标签添加到布局中
        page3_layout.addWidget(self.input3_label3)
        page3_layout.addWidget(self.input3_edit3)

        # 将输入框和标签添加到布局中
        page3_layout.addWidget(self.input4_label3)
        page3_layout.addWidget(self.input4_edit3)

        # # 将确认按钮添加到布局中
        page3_layout.addWidget(self.confirm_button3)

        page3.setLayout(page3_layout)

        # 将页面添加到 stacked widget 中
        stacked_widget.addWidget(page1)
        stacked_widget.addWidget(page2)
        stacked_widget.addWidget(page3)

        # 默认显示第一个页面
        stacked_widget.setCurrentIndex(0)

        # 绑定按钮的点击事件,实现页面切换
        button1.clicked.connect(lambda: stacked_widget.setCurrentIndex(0))
        button2.clicked.connect(lambda: stacked_widget.setCurrentIndex(1))
        button3.clicked.connect(lambda: stacked_widget.setCurrentIndex(2))

        # 设置 stacked widget 为主窗口的中心部件
        main_layout.addLayout(button_layout)
        main_layout.addWidget(stacked_widget)

        # 创建一个 widget 来容纳整个布局
        widget = QWidget()
        widget.setLayout(main_layout)

        # 设置 widget 为主窗口的中心部件
        self.setCentralWidget(widget)

    # 生成负荷曲线确认按钮
    def confirm_loadprofile(self):
        # 获取输入框的值并进行处理
        # 负荷曲线条数
        input1_value = self.input1_edit.text()
        # 负荷曲线间隔
        input2_value = self.input2_edit.text()
        # 设置起始时间
        input3_value = self.input3_edit.text()
        # 设置周格式
        input4_value = self.input4_edit.text()

        file_path, _ = QFileDialog.getSaveFileName(self, "选择文件保存路径", "", "Text Files (*.txt)")
        if file_path:
            print("保存文件路径:", file_path)

        str1 = "写*0008*0000010000FF*02*09*0C"
        str2 = "008000FF*写时钟"

        strtime = input3_value
        #  设置文件头标识
        fpath = file_path
        with open(fpath, 'a+', encoding='utf-8') as f:
            f.write("Operate Type*Class ID*OBIS(6字节16进制)*属性*数据类型(16进制)*注释\n")
            # f.write("\n")

        with open(fpath, 'a+', encoding='utf-8') as f:
            for i in range(0, int(input1_value)):
                # 负荷曲线捕获周期 input2_value min
                delta = datetime.timedelta(minutes=int(input2_value))
                date = datetime.datetime.strptime(strtime, "%Y-%m-%d %H:%M:%S") + delta
                strtime = date.strftime("%Y-%m-%d %H:%M:%S")
                w_week = int(input4_value)
                if w_week == 0:
                    datehex = now_time_tohex_string_weekday(date)
                else:
                    datehex = now_time_tohex_string_isoweekday(date)
                datehex = str1 + datehex + str2
                numop = "//第" + str(i + 1) + "次"
                f.write(datehex)
                f.write("\n")
                f.write(numop)
                f.write("\n")
                f.write("5500\n")

        fpath1 = fpath.replace(".txt", "-1.txt")
        utf8toansi(fpath, fpath1)
        # 确保文件存在
        if os.path.exists(fpath):
            # 删除文件
            os.remove(fpath)
            # print("文件删除成功 ")
        else:
            # print("文件不存在!")
            pass

        msg_box = QMessageBox(self)
        msg_box.setWindowTitle("提示")
        msg_box.setText("文本生成完毕!")
        msg_box.exec_()

    # 生成日冻结确认按钮
    def confirm_daily(self):
        # 获取输入框的值并进行处理
        # 日冻结条数
        input1_value = self.input1_edit2.text()
        # 设置起始时间
        input3_value = self.input3_edit2.text()
        # 设置周格式
        input4_value = self.input4_edit2.text()

        file_path, _ = QFileDialog.getSaveFileName(self, "选择文件保存路径", "", "Text Files (*.txt)")
        if file_path:
            print("保存文件路径:", file_path)

        str1 = "写*0008*0000010000FF*02*09*0C"
        str2 = "008000FF*写时钟"

        strtime = input3_value
        #  设置文件头标识
        fpath = file_path
        with open(fpath, 'a+', encoding='utf-8') as f:
            f.write("Operate Type*Class ID*OBIS(6字节16进制)*属性*数据类型(16进制)*注释\n")
            # f.write("\n")

        with open(fpath, 'a+', encoding='utf-8') as f:
            for i in range(0, int(input1_value)):
                # 日冻结 - 天数+1
                delta = datetime.timedelta(days=1)
                date = datetime.datetime.strptime(strtime, "%Y-%m-%d %H:%M:%S") + delta
                strtime = date.strftime("%Y-%m-%d %H:%M:%S")

                next_day = date.replace(hour=23, minute=59, second=57)

                w_week = int(input4_value)
                if w_week == 0:
                    datehex = now_time_tohex_string_weekday(next_day)
                else:
                    datehex = now_time_tohex_string_isoweekday(next_day)

                datehex = str1 + datehex + str2
                numop = "//第" + str(i + 1) + "次"
                f.write(datehex)
                f.write("\n")
                f.write(numop)
                f.write("\n")
                f.write("5500\n")

        fpath1 = fpath.replace(".txt", "-1.txt")
        utf8toansi(fpath, fpath1)
        # 确保文件存在
        if os.path.exists(fpath):
            # 删除文件
            os.remove(fpath)
            # print("文件删除成功 ")
        else:
            # print("文件不存在!")
            pass

        msg_box = QMessageBox(self)
        msg_box.setWindowTitle("提示")
        msg_box.setText("文本生成完毕!")
        msg_box.exec_()

    # 生成月结算确认按钮
    def confirm_billing(self):
        # 获取输入框的值并进行处理
        # 月结算条数
        input1_value = self.input1_edit3.text()
        # 设置起始时间
        input3_value = self.input3_edit3.text()
        # 设置周格式
        input4_value = self.input4_edit3.text()

        file_path, _ = QFileDialog.getSaveFileName(self, "选择文件保存路径", "", "Text Files (*.txt)")
        if file_path:
            print("保存文件路径:", file_path)

        str1 = "写*0008*0000010000FF*02*09*0C"
        str2 = "008000FF*写时钟"

        strtime = input3_value
        #  设置文件头标识
        fpath = file_path
        with open(fpath, 'a+', encoding='utf-8') as f:
            f.write("Operate Type*Class ID*OBIS(6字节16进制)*属性*数据类型(16进制)*注释\n")
            # f.write("\n")

        with open(fpath, 'a+', encoding='utf-8') as f:
            for i in range(0, int(input1_value)):
                # 月结算---month+1
                delta = relativedelta(months=1)
                date = datetime.datetime.strptime(strtime, "%Y-%m-%d %H:%M:%S") + delta

                # 更新下一次进入循环时的strtime
                strtime = date.strftime("%Y-%m-%d %H:%M:%S")

                year = date.year
                month = date.month

                last_day = datetime.date(year + int(month / 12), (month % 12) + 1, 1) - datetime.timedelta(days=1)
                next_month_last_day = date.replace(day=last_day.day, hour=23, minute=59, second=57)

                w_week = int(input4_value)
                if w_week == 0:
                    datehex = now_time_tohex_string_weekday(next_month_last_day)
                else:
                    datehex = now_time_tohex_string_isoweekday(next_month_last_day)
                datehex = str1 + datehex + str2
                numop = "//第" + str(i + 1) + "次"
                f.write(datehex)
                f.write("\n")
                f.write(numop)
                f.write("\n")
                f.write("5500\n")

        fpath1 = fpath.replace(".txt", "-1.txt")
        utf8toansi(fpath, fpath1)
        # 确保文件存在
        if os.path.exists(fpath):
            # 删除文件
            os.remove(fpath)
            # print("文件删除成功 ")
        else:
            # print("文件不存在!")
            pass

        msg_box = QMessageBox(self)
        msg_box.setWindowTitle("提示")
        msg_box.setText("文本生成完毕!")
        msg_box.exec_()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值