Python:将文本从 HTML 或文本文档导入 Word
Python: Import text from HTML or text document into Word
我一直在查看一些文档,但我所看到的关于 docx 的所有工作主要是针对 with 已经在 word 文档中的文本。我想知道的是,有没有一种简单的方法可以从 HTML 或文本文档中获取文本,然后将其导入到 word 文档中,然后进行批量处理? HTML/Text 文档中的所有文本?好像不太喜欢这个字符串,太长了
我对文档的理解是,您必须逐段处理文本。我想完成的任务相对简单 - 但它超出了我 python 的技能范围。我想在word文档上设置边距,然后将文本导入到word文档中,使其符合我之前指定的边距。
有人有什么想法吗? None 我发现以前的帖子对我很有帮助。
import textwrap
import requests
from bs4 import BeautifulSoup
from docx import Document
from docx.shared import Inches
class DocumentWrapper(textwrap.TextWrapper):
def wrap(self, text):
split_text = text.split('\n\n')
lines = [line for para in split_text for line in textwrap.TextWrapper.wrap(self, para)]
return lines
page = requests.get("http://classics.mit.edu/Aristotle/prior.mb.txt")
soup = BeautifulSoup(page.text,"html.parser")
#we are going to pull in the text wrap extension that we have added.
#The typical width that we want tow
text_wrap_extension = DocumentWrapper(width=82,initial_indent="",fix_sentence_endings=True)
new_string = text_wrap_extension.fill(page.text)
final_document = "Prior_Analytics.txt"
with open(final_document, "w") as f:
f.writelines(new_string)
document = Document(final_document)
### Specified margin specifications
sections = document.sections
for section in sections:
section.top_margin = (Inches(1.00))
section.bottom_margin = (Inches(1.00))
section.right_margin = (Inches(1.00))
section.left_margin = (Inches(1.00))
document.save(final_document)
我抛出的错误如下:
docx.opc.exceptions.PackageNotFoundError: Package not found at 'Prior_Analytics.txt'
此错误仅表示在您指定的位置没有 .docx
文件。因此您可以修改代码以创建它不存在的文件。
final_document = "Prior_Analytics.txt"
with open(final_document, "w+") as f:
f.writelines(new_string)
您提供的是相对路径。你怎么知道 Python 的当前工作目录是什么?这就是你给出的相对路径的起点。
像这样的几行代码将告诉您:
import os
print(os.path.realpath('./'))
注意:
docx
is used to open .docx
files
我知道了。
document = Document()
sections = document.sections
for section in sections:
section.top_margin = Inches(2)
section.bottom_margin = Inches(2)
section.left_margin = Inches(2)
section.right_margin = Inches(2)
document.add_paragraph(###Add your text here. Add Paragraph Accepts text of whatever size.###)
document.save()#name of document goes here, as a string.
我一直在查看一些文档,但我所看到的关于 docx 的所有工作主要是针对 with 已经在 word 文档中的文本。我想知道的是,有没有一种简单的方法可以从 HTML 或文本文档中获取文本,然后将其导入到 word 文档中,然后进行批量处理? HTML/Text 文档中的所有文本?好像不太喜欢这个字符串,太长了
我对文档的理解是,您必须逐段处理文本。我想完成的任务相对简单 - 但它超出了我 python 的技能范围。我想在word文档上设置边距,然后将文本导入到word文档中,使其符合我之前指定的边距。
有人有什么想法吗? None 我发现以前的帖子对我很有帮助。
import textwrap
import requests
from bs4 import BeautifulSoup
from docx import Document
from docx.shared import Inches
class DocumentWrapper(textwrap.TextWrapper):
def wrap(self, text):
split_text = text.split('\n\n')
lines = [line for para in split_text for line in textwrap.TextWrapper.wrap(self, para)]
return lines
page = requests.get("http://classics.mit.edu/Aristotle/prior.mb.txt")
soup = BeautifulSoup(page.text,"html.parser")
#we are going to pull in the text wrap extension that we have added.
#The typical width that we want tow
text_wrap_extension = DocumentWrapper(width=82,initial_indent="",fix_sentence_endings=True)
new_string = text_wrap_extension.fill(page.text)
final_document = "Prior_Analytics.txt"
with open(final_document, "w") as f:
f.writelines(new_string)
document = Document(final_document)
### Specified margin specifications
sections = document.sections
for section in sections:
section.top_margin = (Inches(1.00))
section.bottom_margin = (Inches(1.00))
section.right_margin = (Inches(1.00))
section.left_margin = (Inches(1.00))
document.save(final_document)
我抛出的错误如下:
docx.opc.exceptions.PackageNotFoundError: Package not found at 'Prior_Analytics.txt'
此错误仅表示在您指定的位置没有 .docx
文件。因此您可以修改代码以创建它不存在的文件。
final_document = "Prior_Analytics.txt"
with open(final_document, "w+") as f:
f.writelines(new_string)
您提供的是相对路径。你怎么知道 Python 的当前工作目录是什么?这就是你给出的相对路径的起点。
像这样的几行代码将告诉您:
import os
print(os.path.realpath('./'))
注意:
docx
is used to open.docx
files
我知道了。
document = Document()
sections = document.sections
for section in sections:
section.top_margin = Inches(2)
section.bottom_margin = Inches(2)
section.left_margin = Inches(2)
section.right_margin = Inches(2)
document.add_paragraph(###Add your text here. Add Paragraph Accepts text of whatever size.###)
document.save()#name of document goes here, as a string.