首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SVG文件没有压缩Python中的一些SVG文件

SVG文件没有压缩Python中的一些SVG文件
EN

Stack Overflow用户
提问于 2016-04-12 07:06:02
回答 1查看 203关注 0票数 1

我一直在制作一个脚本,将Latex代码转换为epubs中的SVG图像。这样做的目的是提取临时目录中的epub,查找代码并创建SVG,并替换指向SVG图像的链接,然后再次压缩所有内容。

除了最后的压缩外,一切都很好。它压缩了所有东西,除了我创建的新SVG (我已经检查了它们在时间上未压缩的epub的图片文件夹中)。下面是一个最低限度的工作示例:

代码语言:javascript
运行
复制
import zipfile
import os
import shutil

def create_minimal_uncompressed_epub(directory):
    if os.path.exists(directory):
        shutil.rmtree(directory, ignore_errors=True)
    os.makedirs(directory)
    with open(os.path.join(directory, 'mimetype'), 'w') as mimetype:
        mimetype.write('application/epub+zip')
    os.makedirs(os.path.join(directory, 'META-INF'))
    os.makedirs(os.path.join(directory, 'OEBPS'))
    with open(os.path.join(directory, 'META-INF', 'container.xml'), 'w') as container_xml:
        data = ('<?xml version="1.0"?>'
                '<container version="1.0" xmlns="urn:oasis:names:'
                'tc:opendocument:xmlns:container">'
                '<rootfiles>'
                '<rootfile full-path="OEBPS/content.opf" media-type='
                '"application/oebps-package+xml"/>'
                '</rootfiles>'
                '</container>')
        container_xml.write(data)
    with open(os.path.join(directory, 'OEBPS', 'content.opf'), 'w') as content_opf:
        data = ('<?xml version="1.0" encoding="UTF-8" ?><package xmlns='
                '"http://www.idpf.org/2007/opf" xmlns:dc="http://purl.o'
                'rg/dc/elements/1.1/" unique-identifier="db-id" version'
                '="3.0"><metadata><dc:title id="t1">Title</dc:title><dc'
                ':identifier id="db-id">isbn</dc:identifier><meta   pro'
                'perty="dcterms:modified">2014-03-27T09:14:09Z</meta><d'
                'c:language>en</dc:language></metadata><manifest><item '
                'id="toc" properties="nav" href="toc.xhtml" media-type='
                '"application/xhtml+xml" /><item id="ncx" href="toc.ncx'
                '" media-type="application/x-dtbncx+xml" /><item id="te'
                'mplate_css" href="template.css" media-type="text/css" '
                '/><item id="hello" href="1_hello.xhtml" media-type="ap'
                'plication/xhtml+xml" /></manifest><spine toc="ncx"><it'
                'emref idref="hello" /></spine></package>')
        content_opf.write(data)
    with open(os.path.join(directory, 'OEBPS', 'toc.xhtml'), 'w') as toc_xhtml:
        data = ('<?xml version="1.0" encoding="utf-8"?><html '
                'xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="htt'
                'p://www.idpf.org/2007/ops"><head><title>toc.xhtml</t'
                'itle><link href="template.css" rel="stylesheet" type'
                '="text/css" /></head><body><nav id="toc" epub:type="'
                'toc"><h1 class="frontmatter">Table of Contents</h1><'
                'ol class="contents"><li><a href="1_hello.xhtml">Hell'
                'o</a></li></ol></nav></body></html>')
        toc_xhtml.write(data)
    with open(os.path.join(directory, 'OEBPS', 'toc.ncx'), 'w') as toc_ncx:
        data = ('<?xml version="1.0" encoding="UTF-8" ?><ncx version="2005'
                '-1" xml:lang="en" xmlns="http://www.daisy.org/z3986/2005/'
                'ncx/"><head><meta name="dtb:uid" content="isbn"/><meta na'
                'me="dtb:depth" content="1"/></head><docTitle><text></text'
                '></docTitle><navMap><navPoint id="hello" playOrder="1"><n'
                'avLabel><text>cover</text></navLabel><content src="1_hell'
                'o.xhtml" /></navPoint></navMap></ncx>')
        toc_ncx.write(data)
    with open(os.path.join(directory, 'OEBPS', '1_hello.xhtml'), 'w') as hello_xhtml:
        data = ('<?xml version="1.0" encoding="utf-8"?><html xmlns="http://www.w3.or'
                'g/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"><head><titl'
                'e>1_hello.xhtml</title><link href="template.css" rel="stylesheet" t'
                'ype="text/css" /></head><body><h1>Hello World!</h1></body></html> ')
        hello_xhtml.write(data)
    with open(os.path.join(directory, 'OEBPS', 'template.css'), 'w') as templace_css:
        data = ('h1 {text-align: center;}')
        templace_css.write(data)

def recursive_zip(zipf, directory, folder=None):
    nodes = os.listdir(directory)
    print nodes
    for item in nodes:
        if os.path.isfile(os.path.join(directory, item)):
            zipf.write(os.path.join(directory, item), os.path.join(folder, item), zipfile.ZIP_DEFLATED)
        elif os.path.isdir(os.path.join(directory, item)):
            recursive_zip(zipf, os.path.join(directory, item), os.path.join(folder, item))

def create_svg():
    return 'code here\n'

TEMP_DIR = 'minimal_temp_dir'
SVG_FILENAME = 'minimal_svg_filename.svg'
create_minimal_uncompressed_epub(TEMP_DIR)
with open(os.path.join(TEMP_DIR, 'OEBPS', SVG_FILENAME), 'w') as svgfile:
    svgfile.write(create_svg())
try:
    MINIMAL_EPUB = 'minimal_epub.epub'
    ZIPF = zipfile.ZipFile(MINIMAL_EPUB, 'w')
    ZIPF.write(os.path.join(TEMP_DIR, 'mimetype'), 'mimetype', zipfile.ZIP_STORED)
    for item in os.listdir(TEMP_DIR):
        if os.path.isdir(os.path.join(TEMP_DIR, item)):
            recursive_zip(ZIPF, os.path.join(TEMP_DIR, item), item)
    ZIPF.close()
except: #IOError
    print('\nError compressing file')

函数recursive_zip实际上查找每个文件(注意其中的“打印节点”)。不知道为什么svg文件不见了。没有错误。svg文件在临时文件夹中,但当我用Sigil打开它时,它不在压缩的最终版本中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-16 19:54:46

我终于弄明白了到底发生了什么。我在content.opf文件中,在一个名为manifest的标记中,并根据国际数字出版论坛找到了一个图像列表

所需的清单必须提供出版物一部分的所有文件的列表(例如内容文档、样式表、图像文件、任何嵌入的字体文件、任何包含的架构)。

因此,这些文件实际上被压缩并包含在zip文件中,但是由于它被重命名为.epub并使用Sigil打开,所以SVG图像没有显示,因为它们没有包含在文件content.opf中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36565929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档