目录
场景:默写的时候编辑txt文件,然后转换到word文档中,中翻英或英翻中,省得我来报默了
一、每次处理两行数据
- 我们使用
open
函数读取txt文件的内容,并将其存储在lines
列表中。- 然后,我们遍历
lines
列表,每次处理两行数据,并将它们格式化为指定的样式。- 最后,我们使用
docx
库将格式化后的内容添加到Word文档中,并保存文档。
from docx import Document
# 创建一个新的Word文档
doc = Document()
# 读取txt文件
with open('input.txt', 'r', encoding='GBK') as file:
lines = file.readlines()
# 处理每一行数据
for i in range(0, len(lines), 2):
line1 = lines[i].strip()
line2 = lines[i+1].strip() if i+1 < len(lines) else ''
formatted_line = f"{line1} _____________________________ {line2} _____________________________"
doc.add_paragraph(formatted_line)
# 保存Word文档
doc.save('output.docx')
二、词组一行2个/单词一行4个
txt文件编辑注意:
下划线行上方的为单词,下方的为词组
# -*- coding: utf-8 -*-
# @Author: GraceJiang
# @Date: 2024/7/10
# @Description:
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt
# 创建一个新的Word文档
doc = Document()
# 下划线
underline1=' ________________ '
underline2=' __________________________________ '
# 读取txt文件
with open('input.txt', 'r', encoding='GBK') as file:
lines = file.readlines()
p=doc.add_paragraph()
run = p.add_run('八上英语单词及词组默写')
run.bold = True # 设置为加粗
run.font.size = Pt(12) # 设置字体大小
p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 居中对齐
# 初始化变量
buffer=[]
bufferstatus=False # 是否到----行
formatdata=''
# -----该行之前的数据每4个一行输出,该行之后的数据每2个一行输出
for i in range(0,len(lines)):
data=lines[i].strip() #获取每行的数据
if data.startswith('---'):
for item in range(len(buffer)):
formatdata+=buffer[item]+underline1
doc.add_paragraph(f'{formatdata}')
buffer=[]
bufferstatus=True
continue
else:
buffer.append(lines[i].strip())
if len(buffer)==4:
formatdata=''
for item in range(4):
formatdata+=buffer[item]+underline1
doc.add_paragraph(f"{formatdata}")
buffer = []
# 判断是否达到词组行了
if bufferstatus:
if len(buffer)==2:
formatdata = ''
for item in range(2):
formatdata += buffer[item] + underline2
doc.add_paragraph(f"{formatdata}")
buffer = []
# 如果词组buffer没有达到2个,但是文档到结尾了,也是需要输出的
if i+1==len(lines) and len(buffer)==1:
doc.add_paragraph(f"{buffer[0]+underline2}")
# 保存Word文档
doc.save('output.docx')
三、格式调整
# -*- coding: utf-8 -*-
# @Author: GraceJiang
# @Date: 2024/7/10
# @Description:
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt
# 创建一个新的Word文档
doc = Document()
# 下划线
underline1=' ________________ '
underline2=' __________________________________ '
# 读取txt文件
with open('input.txt', 'r', encoding='GBK') as file:
lines = file.readlines()
p=doc.add_paragraph()
run = p.add_run('八上英语单词及词组默写')
run.bold = True # 设置为加粗
run.font.size = Pt(12) # 设置字体大小
p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 居中对齐
doc.add_paragraph('【单词默写】')
# 初始化变量
buffer=[]
bufferstatus=False # 是否到----行
formatdata=''
# -----该行之前的数据每4个一行输出,该行之后的数据每2个一行输出
for i in range(0,len(lines)):
data=lines[i].strip() #获取每行的数据
#如果达到---开头的行将之前的单词全部输出
if data.startswith('---'):
for item in range(len(buffer)):
formatdata+=buffer[item]+underline1
doc.add_paragraph(f'{formatdata}')
buffer=[]
bufferstatus=True
doc.add_paragraph('【词组默写】')
continue
else:
buffer.append(lines[i].strip())
if len(buffer)==4:
formatdata=''
for item in range(4):
formatdata+=buffer[item]+underline1
doc.add_paragraph(f"{formatdata}")
buffer = []
# 判断是否达到词组行了
if bufferstatus:
if len(buffer)==2:
formatdata = ''
for item in range(2):
formatdata += buffer[item] + underline2
doc.add_paragraph(f"{formatdata}")
buffer = []
# 如果词组buffer没有达到2个,但是文档到结尾了,也是需要输出的
if i+1==len(lines) and len(buffer)==1:
doc.add_paragraph(f"{buffer[0]+underline2}")
# 保存Word文档
doc.save('output.docx')
四、使用函数优化代码【最终脚本】
# -*- coding: utf-8 -*-
# @Author: GraceJiang
# @Date: 2024/7/11
# @Description:
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.shared import Pt
# 创建一个新的Word文档
doc = Document()
# 下划线
UNDERLINE1 = ' ________________ '
UNDERLINE2 = ' __________________________________ '
# 读取txt文件
with open('input.txt', 'r', encoding='GBK') as file:
lines = file.readlines()
def add_title(text, bold=True, size=Pt(12), alignment=WD_PARAGRAPH_ALIGNMENT.CENTER):
p = doc.add_paragraph()
run = p.add_run(text)
run.bold = bold
run.font.size = size
p.alignment = alignment
def add_words(buffer, underline):
formatdata = ''
for item in buffer:
formatdata += item + underline
doc.add_paragraph(formatdata)
add_title('八上英语单词及词组默写')
doc.add_paragraph('【单词默写】')
# 初始化变量
buffer = []
bufferstatus = False # 是否到----行
for line in lines:
data = line.strip()
if data.startswith('---'):
add_words(buffer, UNDERLINE1)
buffer = []
bufferstatus = True
doc.add_paragraph('【词组默写】')
continue
buffer.append(data)
if not bufferstatus and len(buffer) == 4:
add_words(buffer, UNDERLINE1)
buffer = []
if bufferstatus and len(buffer) == 2:
add_words(buffer, UNDERLINE2)
buffer = []
# 处理剩余的单词或词组
if buffer:
add_words(buffer, UNDERLINE2 if bufferstatus else UNDERLINE1)
# 保存Word文档
doc.save('output.docx')