【问题标题】:How do I add newline within text string to zipfile using python 2.7如何使用 python 2.7 在文本字符串中添加换行符到 zipfile
【发布时间】:2013-08-23 22:33:11
【问题描述】:

使用 python 将文本写入 zipfile。下面是我用来执行此操作的代码部分。我不知道我需要使用什么字符才能将新行添加到 zipfile。

if Count:
    TextX = "The following species are present:"
    blurb = "\r\nINSERT TABLE HERE\r\n\r\nSA* - South America\r\nNA** - North America\r\nCA*** - Central America"
else:
    TextX = "No species are present."
    blurb = ""

现在“如果 Count”为真,文档中的输出如下所示:

INSERT TABLE HERE  SA* - South America  NA** - North America  CA*** - Central America

我也希望它看起来像这样:

INSERT TABLE HERE

SA* - South America
NA** - North America
CA*** - Central America

下面是一些其他相关的脚本 sn-p 可能有助于排除故障。该脚本有 600 多行,这就是为什么我没有包括整个内容。除了这件作品外,一切都有效。

replaceText = {"TextSpecies" : TextX,
        "TEXTBLURB" : blurb}

template = zipfile.ZipFile("C:\\Template.docx")
response = zipfile.ZipFile(results, "a")

with open(template.extract("word/document.xml", databaseDir + "\\")) as tempXmlFile:
    tempXml = tempXmlFile.read()

for key in replaceText.keys():
    tempXml = tempXml.replace(str(key), str(replaceText.get(key)))

with open(databaseDir + "\\temp.xml", "w+") as tempXmlFile:
    tempXmlFile.write(tempXml)

for file in template.filelist:
    if not file.filename == "word/document.xml":
        response.writestr(file.filename, template.read(file))

response.write(databaseDir + "\\temp.xml", "word/document.xml")

response.close()
template.close()

关于如何添加新行的想法?我试过\r、\n、\r\n、^11。没有任何效果。

提前致谢。

【问题讨论】:

    标签: python-2.7 newline zipfile


    【解决方案1】:

    根据您提供的详细信息,很明显您想要创建一个 Word DOCX 文件。虽然 docx 文件是一个 zip 文件,但它是一个 zip 文件,具有非常具体的内容和该内容的特定规则。找到的大多数文件都是 XML 文件,word/document.xml 也是如此。在 XML 文件中,空格(包括换行符,无论它们是 Unix \n 还是 Windows 的 \r\n)通常是无关紧要的。相反,您必须创建 Word 期望的所有标签并用合理的数据填充它们。

    我在这里放了一个非常小的word/document.xml 文件,里面有两段,所以你明白我在说什么(Word 通常将这些文件写成一行,不带任何空格,我在这里对其进行了格式化以便于阅读) :

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
        <w:body>
            <w:p>
                <w:pPr>
                    <w:pStyle w:val="style0"/>
                </w:pPr>
                <w:r>
                    <w:rPr></w:rPr>
                    <w:t>This is the first paragraph.</w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:pPr>
                    <w:pStyle w:val="style0"/>
                </w:pPr>
                <w:r>
                    <w:rPr></w:rPr>
                    <w:t>This is the second paragraph.</w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:pPr>
                    <w:pStyle w:val="style0"/>
                </w:pPr>
                <w:r>
                    <w:rPr></w:rPr>
                </w:r>
            </w:p>
            <w:sectPr>
                <w:type w:val="nextPage"/>
                <w:pgSz w:h="16838" w:w="11906"/>
                <w:pgMar w:bottom="1134" w:footer="0" w:gutter="0" w:header="0" w:left="1134" w:right="1134" w:top="1134"/>
                <w:pgNumType w:fmt="decimal"/>
                <w:formProt w:val="false"/>
                <w:textDirection w:val="lrTb"/>
            </w:sectPr>
        </w:body>
    </w:document>
    

    似乎每一行都是这样一个&lt;w:p&gt;标签,对于一个新段落,需要创建一个该标签的新实例,并填写所有信息。

    【讨论】:

    • 我可以看到我没有像我需要的那样清楚。我有一个名为“Automated Response.docx”的 MS Word 模板文件,它在两个名为“TEXTBLURB”的段落之间嵌入了一个标签。使用python脚本,如果满足某个条件,用一些包含换行符的文本替换这个标签,否则,不要在段落之间添加文本。我无法弄清楚将换行符嵌入标签所需的 python 代码。我对这一切仍然很陌生,并且仍然熟悉正确的语法,所以如果我不够清楚,我很抱歉
    • 按照我写的内容,您可以尝试插入 '&lt;/w:t&gt;&lt;/w:r&gt;&lt;/w:p&gt;&lt;w:p&gt;&lt;w:pPr&gt;&lt;w:pStyle w:val="style0"/&gt;&lt;/w:pPr&gt;&lt;w:r&gt;&lt;w:rPr&gt;/w:rPr&gt;&lt;w:t&gt;' 而不是 '\n'。不太确定,但值得一试。
    猜你喜欢
    • 2014-01-13
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    相关资源
    最近更新 更多