[Python] 纯文本查看 复制代码
# uncompyle6 version 3.9.2
# Python bytecode version base 3.8.0 (3413)
# Decompiled from: Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
# Embedded file name: TXT转换WORD.py
import os, docx
from docx.enum.text import WD_COLOR_INDEX
from docx.oxml.ns import qn
from docx.shared import Pt
from docx.shared import RGBColor
cwd = os.getcwd()
input_file = os.path.join(cwd, "题库.txt")
output_file = os.path.join(cwd, "题库.docx")
while True:
while True:
input_filename = input("请输入需要调整的txt文件路径(默认为当前目录):" + input_file + "\n")
output_filename = input("请输入调整后txt文件的路径(默认为当前目录):" + output_file + "\n")
title = input("请输入WORD文档标题(默认为“单选题”):\n")
if len(str(title)) == 0:
title = "单选题"
if len(str(input_filename)) == 0:
input_filename = input_file
if len(str(output_filename)) == 0:
output_filename = output_file
if os.path.isfile(input_filename):
break
else:
print("路径输入错误,请重新输入!")
rgbs = {'黑色': (0, 0, 0),
'红色': (255, 0, 0),
'蓝色': (0, 0, 255),
'黄色': (255, 255, 0),
'青色': (0, 255, 255),
'品红': (0, 255, 255),
'CornflowerBlue': (100, 149, 237)}
filr = 1
print("可选颜色共有以下几种(请输入颜色名作为选择):")
for rgb in rgbs.keys():
print(str(filr) + "." + rgb)
filr += 1
else:
p1 = input("请选择题目颜色...键入示例:黑色\n")
if p1 in rgbs.keys():
key_rgb = RGBColor(*rgbs[p1])
else:
key_rgb = RGBColor(100, 149, 237)
print("使用默认颜色...\n")
p2 = input("请选择答案选项颜色...\n")
if p2 in rgbs.keys():
value_rgb = RGBColor(*rgbs[p2])
else:
value_rgb = RGBColor(255, 0, 0)
print("使用默认颜色...\n")
document = docx.Document()
document.add_heading(title, 0)
document.styles["Normal"].font.name = "宋体"
document.styles["Normal"]._element.rPr.rFonts.set(qn("w:eastAsia"), "宋体")
values = list()
key = ""
flag = False
value_right_rgb = WD_COLOR_INDEX.YELLOW
def insert_docx(key_rgb, value_rgb, value_right_rgb, key, values):
p = document.add_paragraph()
key_run = p.add_run(key)
key_run.font.color.rgb = key_rgb
key_run.font.size = Pt(12)
key_run.font.bold = True
for m in values:
if "(不选)" in m:
m = m.replace("(不选)", "")
run = p.add_run(m)
run.font.color.rgb = value_rgb
elif "(选)" in m:
m = m.replace("(选)", "")
run = p.add_run(m)
run.font.color.rgb = value_rgb
run.font.highlight_color = value_right_rgb
else:
run = p.add_run(m)
with open(input_filename, encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
if "." in line[0[:4]]:
num_s = line.split(".")
else:
if "、" in line[0[:4]]:
num_s = line.split("、")
else:
num_s = line
try:
if len(num_s) >= 2 and num_s[0].isdigit():
if flag:
insert_docx(key_rgb, value_rgb, value_right_rgb, key, values)
flag = False
values = list()
key = line
else:
values.append(line)
flag = True
except Exception as e:
try:
print(e, line)
values = list()
key = ""
flag = False
finally:
e = None
del e
else:
document.save(output_filename)
print("操作已成功完成,请到输出目录下查看!")
os.system("pause")
# okay decompiling TXT转换WORD.pyc