首先,我必须说,我对编程非常陌生(总共不到一周的经验)。我开始编写一个生成一系列.odt模板文档的程序。我想使用带有特定关键字的模板,比如"X1234X“等等。然后,它将被从程序生成的值替换。每个文档有一点不同,值是通过提示符(日期和其他事情)输入和计算的。
到目前为止,大部分代码都是我写的,但是我已经在这个问题上花了两天时间了。我使用ezodf模块从一个模板生成一个新文档(具有不同的文件名),但是我一直在讨论如何编辑内容。我在谷歌上搜索了很久,却发现这里的人能帮上忙。我试着阅读文档,但我一定是honest...its有点难理解。我不熟悉“俚语”
谢谢
PS: ezodf方法很好,但是任何其他方法都可以。这个程序不必很漂亮,只需要工作(这样我就可以少做^_^了)
发布于 2015-06-05 00:09:10
我想出来了。并完成了程序。我使用ezodf创建文件,然后使用zipfile提取和编辑content.xml,然后通过here的一个好的>def的thingy<重新打包整个过程。我试着和etree...but混在一起,我想不出.
from ezodf import newdoc
import os
import zipfile
import tempfile
for s in temp2:
input2 = s
input2 = str(s)
input1 = cname[0]
file1 = '.odt'
namef = input2 + input1 + file1
odt = newdoc(doctype='odt', filename=namef, template='template.odt')
odt.save()
a = zipfile.ZipFile('template.odt')
content = a.read('content.xml')
content = str(content.decode(encoding='utf8'))
content = str.replace(content,"XXDATEXX", input2)
content = str.replace(content, 'XXNAMEXX', input1)
def updateZip(zipname, filename, data):
# generate a temp file
tmpfd, tmpname = tempfile.mkstemp(dir=os.path.dirname(zipname))
os.close(tmpfd)
# create a temp copy of the archive without filename
with zipfile.ZipFile(zipname, 'r') as zin:
with zipfile.ZipFile(tmpname, 'w') as zout:
zout.comment = zin.comment # preserve the comment
for item in zin.infolist():
if item.filename != filename:
zout.writestr(item, zin.read(item.filename))
# replace with the temp archive
os.remove(zipname)
os.rename(tmpname, zipname)
# now add filename with its new data
with zipfile.ZipFile(zipname, mode='a', compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr(filename, data)
updateZip(namef, 'content.xml', content)https://stackoverflow.com/questions/30596477
复制相似问题