我的django网络应用程序制作并保存docx,我需要让它可下载.我用简单render_to_response如下.
return render_to_response("test.docx", mimetype='application/vnd.ms-word')
Run Code Online (Sandbox Code Playgroud)
但是,它会引发错误 'utf8' codec can't decode byte 0xeb in position 15: invalid continuation byte
我无法将此文件作为静态服务,所以我需要找到一种方法来为它提供服务.真的很感激任何帮助.
我需要通过Python 3.4在Word文档中创建表.为此,我试图python-docx在Windows中安装.如果我使用pip install python-docx我收到以下错误:
vcvarsall.bat error
Run Code Online (Sandbox Code Playgroud)
所以我安装了Visual Studio,然后尝试安装它,我仍然得到以下内容:
error: Setup script exited with error: command '"C:\Program Files (x86)\Microsoft Visual
Studio 10.0\VC\BIN\cl.exe"' failed with exit status 2
Run Code Online (Sandbox Code Playgroud) 我正在python 2.7 Python-docx尝试修改 Microsoft docx 文档中表格中单元格的内容。我以 xml 格式打开文档,以便查看事物所在的位置并尝试获取值,以便我可以引用它们。下面是我发现的。
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="TableGrid"/>
<w:tblW w:w="0" w:type="auto"/>
<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="1345"/>
<w:gridCol w:w="3148"/>
<w:gridCol w:w="3148"/>
<w:gridCol w:w="3149"/>
</w:tblGrid>
<w:tr w:rsidR="002C543C" w14:paraId="4C33FE0D" w14:textId="77777777" w:rsidTr="009E290C">
<w:trPr>
<w:cantSplit/>
<w:trHeight w:hRule="exact" w:val="1080"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:w="1345" w:type="dxa"/>
</w:tcPr>
<w:p w14:paraId="4497FDDB" w14:textId="77777777" w:rsidR="002C543C" w:rsidRDefault="002C543C">
<w:pPr>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/>
<w:sz w:val="24"/>
<w:szCs w:val="24"/>
</w:rPr>
</w:pPr>
</w:p>
</w:tc>
<w:tc>
<w:tcPr> …Run Code Online (Sandbox Code Playgroud) 我正在使用 Python-docx 创建和编写 Word 文档。
如何使用 python-docx 在文档标题中放置文本?
http://image.prntscr.com/image/8757b4e6d6f545a5ab6a08a161e4c55e.png
谢谢
我正在尝试读取 docx 文件并将文本添加到列表中。现在我需要列表包含来自 docx 文件的行。
例子:
.docx 文件:
"Hello, my name is blabla,
I am 30 years old.
I have two kids."
Run Code Online (Sandbox Code Playgroud)
结果:
['Hello, my name is blabla', 'I am 30 years old', 'I have two kids']
Run Code Online (Sandbox Code Playgroud)
我无法让它工作。
使用docx2txt这里的模块:
github链接
只有一个进程命令,它返回 docx 文件中的所有文本。
我也希望它保留特殊字符,如 ":\-\.\,"
我一直在使用python实现自定义解析器,并使用该解析数据来格式化要在内部分发的word文档。到目前为止,所有格式都非常简单明了,但是我完全对如何在单个表格单元格中插入复选框感到困惑。
我尝试在python-docx中使用python对象函数(使用get_or_add_tcPr()等),这会导致MS Word在尝试打开文件时引发以下错误:“文件xxxx无法打开,因为内容存在问题详细信息:文件已损坏,无法打开”。
经过一段时间的努力,我转向了第二种方法,该方法涉及为输出文档操作word / document.xml文件。我已经检索出我认为是另存为复选框的正确xml,replacementXML并将填充文本插入到单元格中,以用作可以搜索和替换的标签searchXML。以下内容似乎在Linux(Fedora 25)环境中使用python运行,但是当我尝试打开文档时,单词文档显示相同的错误,但是这次文档是可恢复的,并恢复为填充文本。我已经能够将其与手工制作的文档一起使用,并使用一个空的表格单元格,因此我相信这应该是可能的。注意:我在表格单元格中包含了整个xml元素searchXML变量,但我尝试使用正则表达式并缩短字符串。我不仅仅使用精确匹配,因为我知道这可能会因单元而异。
searchXML = r'<w:tc><w:tcPr><w:tcW w:type="dxa" w:w="4320"/><w:gridSpan w:val="2"/></w:tcPr><w:p><w:pPr><w:jc w:val="right"/></w:pPr><w:r><w:rPr><w:sz w:val="16"/></w:rPr><w:t>IN_CHECKB</w:t></w:r></w:p></w:tc>'
def addCheckboxes():
os.system("mkdir unzipped")
os.system("unzip tempdoc.docx -d unzipped/")
with open('unzipped/word/document.xml', encoding="ISO-8859-1") as file:
filedata = file.read()
rep_count = 0
while re.search(searchXML, filedata):
filedata = replaceXML(filedata, rep_count)
rep_count += 1
with open('unzipped/word/document.xml', 'w') as file:
file.write(filedata)
os.system("zip -r ../buildcfg/tempdoc.docx unzipped/*")
os.system("rm -rf unzipped")
def replaceXML(filedata, rep_count):
replacementXML = r'<w:tc><w:tcPr><w:tcW w:w="4320" w:type="dxa"/><w:gridSpan w:val="2"/></w:tcPr><w:p w:rsidR="00D2569D" w:rsidRDefault="00FD6FDF"><w:pPr><w:jc w:val="right"/></w:pPr><w:r><w:rPr><w:sz w:val="16"/>
</w:rPr><w:fldChar w:fldCharType="begin"><w:ffData><w:name …Run Code Online (Sandbox Code Playgroud) 好吧,所以这就是我想要做的.我想轻松地将文本从文本文件转换为word文档.我目前有这个......
from docx import Document
text_file = "pathToYourTextFile.txt"
#opens document to add text to
document = Document()
#adds the entire contents to a list that we will
#then use to add to the document we just created
fileContents = []
for line in open(text_file):
row = line.split(' ')
fileContents += list(row)
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(fileContents)
#saves the document with all the under the name we give it
document.save('test.docx') …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Python 的 python-docx 模块更改多个段落的格式。
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.enum.section import WD_ORIENTATION
from content import report_content, provinces, report_date, introduction, intro_content
alignment_dict = {'justify': WD_PARAGRAPH_ALIGNMENT.JUSTIFY,
'center': WD_PARAGRAPH_ALIGNMENT.CENTER,
'centre': WD_PARAGRAPH_ALIGNMENT.CENTER,
'right': WD_PARAGRAPH_ALIGNMENT.RIGHT,
'left': WD_PARAGRAPH_ALIGNMENT.LEFT}
orientation_dict = {'portrait': WD_ORIENTATION.PORTRAIT,
'landscape': WD_ORIENTATION.LANDSCAPE}
document = Document()
def change_orientation(orientation='portrait', set_left_margin=1.0, set_right_margin=1.0):
section = document.sections[-1]
new_width, new_height = section.page_height, section.page_width
section.orientation = orientation_dict[orientation]
section.page_width = new_width
section.page_height = new_height
section.left_margin = Inches(set_left_margin)
section.right_margin = …Run Code Online (Sandbox Code Playgroud) 给定一个包含段落的文档
d = docx.Document()
p = d.add_paragraph()
Run Code Online (Sandbox Code Playgroud)
我希望每次都能使用以下技术:
if len(p._element) == 0:
# p is empty
Run Code Online (Sandbox Code Playgroud)
要么
if len(p._p) == 0:
# p is empty
Run Code Online (Sandbox Code Playgroud)
(旁边的问题,那有什么区别?似乎p._p is p._element我在野外见过的每种情况。)
如果我在段落中添加样式,则检查将不再起作用:
>>> p2 = d.add_paragraph(style="Normal")
>>> print(len(p2._element))
1
Run Code Online (Sandbox Code Playgroud)
显式设置text=None也无济于事,不是我期望的那样。
那么,如何检查段落是否没有内容(特别是文本和图像,尽管一般性更好)?
更新资料
我弄乱了一点,发现设置样式显然增加了一个pPr元素:
>>> p2._element.getchildren()
[<CT_PPr '<w:pPr>' at 0x7fc9a2b64548>]
Run Code Online (Sandbox Code Playgroud)
元素本身为空:
>>> len(p2._element.getchildren()[0])
0
Run Code Online (Sandbox Code Playgroud)
但更重要的是,这不是一次尝试。
所以我的测试现在看起来像这样:
def isempty(par):
return sum(len(run) for run in par._element.xpath('w:r')) == 0
Run Code Online (Sandbox Code Playgroud)
我对底层系统了解不足,无法知道这是否是一个合理的解决方案,以及警告。
更多更新
似乎我需要能够处理一些不同的情况:
def isempty(par):
p = par._p
runs = p.xpath('./w:r[./*[not(self::w:rPr)]]')
others …Run Code Online (Sandbox Code Playgroud) 我想为MS Word文本中的更正单词设置样式。由于无法在运行中更改文本样式,因此我想在现有段落中插入具有新样式的新运行...
for p in document.paragraphs:
for run in p.runs:
if 'text' in run.text:
new_run= Run()
new_run.text='some new text'
# insert this run into paragraph
# smth like:
p.insert(new_run)
Run Code Online (Sandbox Code Playgroud)
怎么做?
p.add_run() 在段落末尾添加一行,不是吗?
最好的办法是能够克隆运行(并在一定运行后插入)。这样,我们可以在新的/克隆的中复制原始运行的样式属性。
我可以管理该插入代码:
if 'text' in run.text:
new_run_element = CT_R() #._new()
run._element.addnext(new_run_element)
new_run = Run(new_run_element, run._parent)
...
Run Code Online (Sandbox Code Playgroud)
但是之后: