我正在尝试将文字文件的内容复制到列表,以后可以访问该列表以将其插入新的文字文件中。简而言之,我正在尝试执行一个功能,该功能一次可以复制并粘贴到Word文档中的所选内容上。 我遇到的问题是各节之间的空白“行” /间距。此间距对于文档的布局很重要。我可以复制包含对象和数字的行,但是涉及到空行。它吐出一个错误。
我已经在这个论坛和其他论坛中搜索了解决方案,但没有发现与此特定问题相关的任何信息
import tkinter as tk
from tkinter.filedialog import askopenfilename
from docx import Document # Invokes Document command from docx
def get_para_data(output_doc_name, paragraph):
"""
Write the run to the new file and then set its font, bold, alignment,
color etc. data.
"""
output_para = output_doc_name.add_paragraph()
for run in paragraph.runs:
output_run = output_para.add_run(run.text)
# Run's bold data
output_run.bold = run.bold
# Run's italic data
output_run.italic = run.italic
# Run's underline data
output_run.underline = run.underline
# Run's color data
output_run.font.color.rgb = run.font.color.rgb
# Run's font data
output_run.style.name = run.style.name
# Paragraph's alignment data
output_para.paragraph_format.alignment =
paragraph.paragraph_format.alignment
return output_run
n = 4
# IMPORT WORD DOCUMENT
root = tk.Tk()
root.withdraw()
doc_path = askopenfilename(title="Choose Word File") # returns the file
path as variable for future use
document = Document(doc_path) # Imports Word Document to Modify
t = len(document.paragraphs) # gives the number of lines in document
print('Total Number of lines =', t) # this is a check for now
result = Document()
output_paragraph = [None]*t
i = 0
for para in document.paragraphs:
output_paragraph[i] = get_para_data(result, document.paragraphs[i])
i += 1
第一次迭代运行得很好,但是一旦我们到达第二次迭代和空白行,它就会崩溃并给出一些错误。
回溯(最近一次拨打电话):文件“ C:\ Program Files \ JetBrains \ PyCharm社区版 2018.3.2 \ helpers \ pydev \ pydevd.py“,行1741,在 main()
文件“ C:\ Program Files \ JetBrains \ PyCharm社区版 2018.3.2 \ helpers \ pydev \ pydevd.py“,行1735,在主目录中 全局变量= debugger.run(setup ['file'],None,None,is_module)
文件“ C:\ Program Files \ JetBrains \ PyCharm社区版 2018.3.2 \ helpers \ pydev \ pydevd.py“,行1135,正在运行 pydev_imports.execfile(file,globals,locals)#执行脚本
文件“ C:\ Program Files \ JetBrains \ PyCharm社区版 2018.3.2 \ helpers \ pydev_pydev_imps_pydev_execfile.py“,第18行,在execfile中 exec(compile(contents +“ \ n”,file,'exec'),glob,loc
文件“ C:/Users/SmithC113/PycharmProjects/ASCII_Word/point_creator.py”,行 51,在 output_para [i] = get_para_data(result,document.paragraphs [i])
文件“ C:/Users/SmithC113/PycharmProjects/ASCII_Word/point_creator.py”, 第35行,在get_para_data中 返回output_run UnboundLocalError:赋值之前引用了本地变量'output_run'
答案 0 :(得分:0)
添加一个“ if”语句以标识空的段落并分配[None]值。解决了该问题。
import tkinter as tk
from tkinter.filedialog import askopenfilename
from docx import Document # Invokes Document command from docx
def get_para_data(output_doc_name, paragraph):
"""
Write the run to the new file and then set its font, bold, alignment, color etc.
data.
"""
output_run = []
output_para = output_doc_name.add_paragraph()
for run in paragraph.runs:
if paragraph:
output_run = output_para.add_run(run.text)
# Run's bold data
output_run.bold = run.bold
# Run's italic data
output_run.italic = run.italic
# Run's underline data
output_run.underline = run.underline
# Run's color data
output_run.font.color.rgb = run.font.color.rgb
# Run's font data
output_run.style.name = run.style.name
# Paragraph's alignment data
output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
else:
output_run = [None]
return output_run
n = 4
# IMPORT WORD DOCUMENT
root = tk.Tk()
root.withdraw()
doc_path = askopenfilename(title="Choose Word File") # returns the file path as
variable for future use
document = Document(doc_path) # Imports Word Document to Modify
t = len(document.paragraphs) # gives the number of lines in document
print('Total Number of lines =', t) # this is a check for now
result = Document()
output_paragraph = [None]*t
i = 0
for para in document.paragraphs:
output_paragraph[i] = get_para_data(result, document.paragraphs[i])
i += 1