吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3769|回复: 21
收起左侧

[Python 转载] 文件段落排版

  [复制链接]
ymhld 发表于 2020-1-25 18:07
本帖最后由 ymhld 于 2020-1-27 17:29 编辑

一、编排原因:
在悬赏区答复时,因为下载的文件排版格式比较乱,尤其是段落排版不是很好,因此想用刚学的python进行一下排版

二、编排原则:
去掉头尾无用的空格等,自动在行尾遇到。”两种字符,进行段落划分。如遇到行首有“,则将”划分至上一段落。

三、编排处理:
首先做了文字处理部分,使其达到基本要求。
最后加入TK控件,使界面更友好,打开、处理、保存文件、提示等操作更直观。

四、应用注意:
txt文档的处理要用UTF-8格式。
如果有从网上下载的格式比较乱的文档,也可以用此方法进行编排。
在处理的同时,已经存储了,格式为原文档的名字后加个1

代码如下:(修正一处,要不保存是保存两遍的)

#-*-coding:GBK -*-
#


print("                                                              ")
print("      _____    _____      _____  _____                   ____ ")
print(" ___|\     \  |\    \    /    /||\    \   _____         |    |")
print("|    |\     \ | \    \  /    / || |    | /    /|        |    |")
print("|    | |     ||  \____\/    /  /\/     / |    ||        |    |")
print("|    | /_ _ /  \ |    /    /  / /     /_  \   \/  ____  |    |")
print("|    |\    \    \|___/    /  / |     // \  \   \ |    | |    |")
print("|    | |    |       /    /  /  |    |/   \ |    ||    | |    |")
print("|____|/____/|      /____/  /   |\ ___/\   \|   /||\____\|____|")
print("|    /     ||     |`    | /    | |   | \______/ || |    |    |")
print("|____|_____|/     |_____|/      \|___|/\ |    | | \|____|____|")
print(" \(    )/           )/            \(   \|____|/     \(   )/  ")
print("   '    '            '              '      )/         '   '   ")
print("                                           '                 ")
"""

"""
import tkinter as tk
from tkinter import filedialog, dialog,messagebox
import os


window = tk.Tk()
window.title('文件段落整理')  # 标题
window.geometry('600x380+600+200')  # 窗口尺寸
window.resizable(width=False, height=False) #限制改变尺寸
#A9E024
file_path = ''

file_text = ''
dirname=''
#FFFFFF
text1 = tk.Text(window, width=50, height=10, bg='#A9E024', font=('Arial', 12))
text1.place(x=10,y=10,width=580,height=320)



def open_file():
    '''
    打开文件
    :return:
    '''
    global file_path
    global file_text
    global dirname
   
    contents = text1.get(0.0,tk.END).strip()
    if contents !="":
        text1.delete('1.0', tk.END)
    if dirname=="":
        dirname=os.getcwd()
    file_path = filedialog.askopenfilename(title=u'打开文件',
    filetypes=[('TXT', '*.TXT'), ('All Files', '*')],
    initialdir=dirname)
    print('打开文件:', file_path)
    if file_path is not None:
        with open(file=file_path, mode='r+', encoding='UTF-8') as file:
            file_text = file.read()
        text1.insert('insert', file_text)


def save_file():
    global file_path
    global file_text
    global filename1
    global dirname
    contents = text1.get(0.0,tk.END).strip()
    #print (len(contents))
    if contents=="":
        tk.messagebox.showinfo('文件不存在','请打开文件操作!')
        open_file()
        deal_file()
    filename2=os.path.basename(filename1)
    file_path1 = filedialog.asksaveasfilename(title=u'保存文件',
    initialfile=filename2,
    filetypes=[('TXT', '*.TXT'), ('All Files', '*')],
    initialdir=dirname)
    print('保存文件:', file_path1)
    file_text = text1.get('1.0', tk.END)
    if file_path1 is not None:
        with open(file=file_path1, mode='w', encoding='UTF-8') as file:
            file.write(file_text)
        text1.delete('1.0', tk.END)
        tk.messagebox.showinfo('文件存储完毕',file_path1+'保存完成')
        print('保存完成')

def deal_file():
        global file_path
        global file_text
        global dirname
        global filename1
        if file_path=="":
                open_file()
        dirname,filename=os.path.split(file_path)
        print ('处理文件名',filename)
        line1="    "
        with open(file_path,'r',encoding='UTF-8') as file_object:
                lines = file_object.readlines()
        for line in lines:  #lines[0:10]:
                line=line.strip()
                #print(line.strip())
                if len(line)==0:
                        pass
                else:
                        if line[-1:]=="。" or line[-1:]=="”" :
                                line=line+"\n    "
                                #print (line[-4:-1],"***",line[-4:-1],"***")
                        if line[0]=="”":
                                if line1[-4:-1]=="    "[-4:-1]:
                                        line1=line1[:-5]+"”\n    "
                                       
                                else:
                                        line1=line1+"”"
                                line=line[1:]
                        line1=line1+line
        #print(line1)
        text1.delete('1.0', tk.END)
        text1.insert('insert', line1)
        
        filename1,filesuffix = os.path.splitext(filename)
        filename1=os.path.join(dirname,filename1+"1"+filesuffix)
        print (filename1)
        with open(filename1,'w',encoding='UTF-8') as file_object:
                file_object.write(line1)  




bt1 = tk.Button(window, text='打开文件', width=15, height=2,command=open_file)
bt1.place(x=80,y=340,width=70,height=30)
bt3 = tk.Button(window, text='整理文件', width=15, height=2, command=deal_file)
bt3.place(x=265,y=340,width=70,height=30)
#bt3.grid(row=0,column=1)
#bt3.pack()
bt2 = tk.Button(window, text='保存文件', width=15, height=2, command=save_file)
bt2.place(x=450,y=340,width=70,height=30)
#bt3.grid(row=0,column=2)
#bt2.pack()

window.mainloop()  # 显示


编译后的文件:(zip和exe)


https://www.lanzouj.com/i8vrqwj

https://www.lanzouj.com/i8vrqzc


测试用txt

https://www.lanzouj.com/i8uwlhi

应用效果:
GIF 2020-1-25 18-02-40.gif



排版前后比较:

QQ截图20200125180623.png





后记:
今天去优秀会员评选的帖子里去转时,发现了同款软件,一并发给大家
https://www.52pojie.cn/thread-918163-1-1.html

免费评分

参与人数 4吾爱币 +3 热心值 +3 收起 理由
小白1只吖 + 1 + 1 谢谢@Thanks!
gsp418 + 1 热心回复!
三字经123 + 1 + 1 我很赞同!
生有涯知无涯 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| ymhld 发表于 2021-4-1 20:38
[Python] 纯文本查看 复制代码
import pyperclip  # 粘贴板用



lines=pyperclip.paste()

print(lines,len(lines))
line1=""
for line in lines:  #lines[0:10]:
	line=line.strip()
	#print(line.strip())
	# print (line,len(line1))
	if line=="":
		if line1[-1:]=="。" or line1[-1:]=="”" or line1[-1:]==" " or line1[-1:]==" ":
			line="\n"
	line1=line1+line

# print(line1)
pyperclip.copy(line1)

exit()
virgin_8 发表于 2021-3-31 10:05
本帖最后由 virgin_8 于 2021-3-31 10:17 编辑
ymhld 发表于 2021-3-31 00:06
https://wwx.lanzoui.com/iLZ6vnhotve

大概是这个意思吧

就是这样的  太感谢了  这个非常好。

换行和首行缩进4个字符 去掉吧  我word设置了首行缩进了 。
image.png
gsp418 发表于 2020-1-25 18:48
xyw195 发表于 2020-1-25 19:12
挺好的,谢谢
liuwahai 发表于 2020-1-25 20:00
跟天若比,博主可以再接再励
y294945022 发表于 2020-1-25 20:05
虽然没有完全看懂它的用途,不过楼主辛苦了
 楼主| ymhld 发表于 2020-1-25 20:08
liuwahai 发表于 2020-1-25 20:00
跟天若比,博主可以再接再励

练习为主,本来也没想做出个成品,原想处理一下文本就得了
cdwdz 发表于 2020-1-25 21:43
感谢楼主分享  谢谢  
它只能处理txt格式的文件     是吗?
 楼主| ymhld 发表于 2020-1-25 21:46
cdwdz 发表于 2020-1-25 21:43
感谢楼主分享  谢谢  
它只能处理txt格式的文件     是吗?

你想处理什么文件?先转成txt,再回去吧,这样稳妥点,其实只要是文本格式UTF-8的都能可以
cdwdz 发表于 2020-1-25 21:48
ymhld 发表于 2020-1-25 21:46
你想处理什么文件?先转成txt,再回去吧,这样稳妥点,其实只要是文本格式UTF-8的都能可以

哦哦 知道了  谢谢
virgin_8 发表于 2021-3-27 07:39
本帖最后由 virgin_8 于 2021-3-27 07:48 编辑

能不能修改成。
我先复制文本-双击打开你的段落排版工具(不要弹出任何窗口)-到word粘贴就是排好版的内容 ,这样的话工作效率更快捷。
软件更小一点,bat也行。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-29 07:11

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表