Created
November 21, 2018 20:14
-
-
Save albcunha/0d6feb6953e77b93ee3a119fb200ed34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from PIL import Image, ImageDraw | |
import math | |
import os | |
import re | |
import time | |
import zipfile | |
import tempfile | |
import glob | |
from shutil import copy | |
import glob | |
import math | |
def long_slice(image_path, outdir): | |
"""slice an image into parts slice_size tall""" | |
img = Image.open(image_path) | |
width, height = img.size | |
tamanho_linha_imagem = 75 | |
pedaços = math.ceil(height / tamanho_linha_imagem) # como cada linha é 1, não precisa dividir | |
largura_tabela = width | |
upper = 0 | |
left = 0 | |
count = 1 | |
#slices = math.ceil(height/) | |
for slice in range(pedaços): | |
#if we are at the end, set the lower bound to be the bottom of the image | |
if count == pedaços: | |
lower = height | |
else: | |
lower = int(count) * tamanho_linha_imagem | |
#set the bounding box! The important bit. Imagine que ele vai fazendo um quadro que vai descendo e vai recortando a imagem nos parametros acima. | |
bbox = (left, upper, width, lower +1) #you need to add 1 pixel to the bottom or else it miss it on the next iteration (bug?). | |
working_slice = img.crop(bbox) | |
# addd transparent bottom padding to the last image, so the last row wont have distortion | |
if slice == pedaços - 1: | |
blank_image = Image.new('RGBA', (width,tamanho_linha_imagem ), color= (0,0,0,0)) | |
blank_image.paste(working_slice, (0,0)) | |
blank_image.convert('RGBA') | |
working_slice = blank_image | |
upper += 1 * tamanho_linha_imagem | |
#save the slice | |
out_name = re.sub('[\W_]','',image_path) | |
arquivo_saida = os.path.join(outdir, "slice" + out_name + str(count)+".png") | |
lista_arquivos.append(arquivo_saida) | |
working_slice.save(arquivo_saida, quality=100) | |
count +=1 | |
img.close() | |
print("Arquivo: ", image_path) | |
print(' Width',width) | |
print(' Height',height) | |
print(' Pedaços',pedaços) | |
print(' Largura_tabela',largura_tabela) | |
return largura_tabela, tamanho_linha_imagem | |
def adiciona_imagens(zipname, file_jpg): | |
# adiciona o arquivo jpg na pasta PICTURES do zipfile | |
with open(file_jpg,'rb') as f: | |
data= f.read() | |
with zipfile.ZipFile(zipname, mode='a', compression=zipfile.ZIP_DEFLATED) as zf: | |
zf.writestr('Pictures/' + file_jpg, data) | |
def update_zip(zipname, filepath,contents): | |
# 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 filepath == item.filename: | |
with zin.open(item) as f: | |
zout.writestr(filepath, contents) | |
else: | |
zout.writestr(item, zin.read(item.filename)) | |
# replace with the temp archive | |
os.remove(zipname) | |
os.rename(tmpname, zipname) | |
arquivos_para_criação_odt = glob.glob("*.jpg") | |
arquivos_para_criação_odt += glob.glob("*.png") | |
for arquivo_imagem in arquivos_para_criação_odt: | |
lista_arquivos = [] | |
# gera arquivos e linhas | |
largura_tabela, tamanho_linha_imagem = long_slice(arquivo_imagem,os.getcwd()) | |
largura_tabela = 16 | |
tamanho_linha_odt = tamanho_linha_imagem / 75 | |
arquivo_odt = arquivo_imagem[:arquivo_imagem.rfind('.')] + '.odt' | |
copy('arquivo_base.file',arquivo_odt ) | |
contents_xml_inicial = f"""<?xml version="1.0" encoding="UTF-8"?><office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2"><office:scripts/><office:font-face-decls><style:font-face style:name="Lucida Sans1" svg:font-family="'Lucida Sans'" style:font-family-generic="swiss"/><style:font-face style:name="Calibri" svg:font-family="Calibri" style:font-family-generic="roman" style:font-pitch="variable"/><style:font-face style:name="Cambria" svg:font-family="Cambria" style:font-family-generic="roman" style:font-pitch="variable"/><style:font-face style:name="Courier" svg:font-family="Courier" style:font-family-generic="roman" style:font-pitch="variable"/><style:font-face style:name="Liberation Sans" svg:font-family="'Liberation Sans'" style:font-family-generic="swiss" style:font-pitch="variable"/><style:font-face style:name="F" svg:font-family="" style:font-family-generic="system" style:font-pitch="variable"/><style:font-face style:name="Lucida Sans" svg:font-family="'Lucida Sans'" style:font-family-generic="system" style:font-pitch="variable"/><style:font-face style:name="Microsoft YaHei" svg:font-family="'Microsoft YaHei'" style:font-family-generic="system" style:font-pitch="variable"/><style:font-face style:name="MS ゴシック" svg:font-family="'MS ゴシック'" style:font-family-generic="system" style:font-pitch="variable"/><style:font-face style:name="MS 明朝" svg:font-family="'MS 明朝'" style:font-family-generic="system" style:font-pitch="variable"/></office:font-face-decls> | |
<office:automatic-styles> | |
<style:style style:name="Tabela1" style:family="table"> | |
<style:table-properties style:writing-mode="lr-tb" table:align="center" fo:margin-bottom="0cm" fo:margin-top="0cm" fo:margin-left="-0.191cm" style:width="{largura_tabela}cm"/> | |
</style:style> | |
<style:style style:name="Tabela1.A" style:family="table-column"> | |
<style:table-column-properties style:column-width="{largura_tabela}cm"/> | |
</style:style> | |
<style:style style:name="Tabela1.1" style:family="table-row"> | |
<style:table-row-properties fo:keep-together="auto" style:row-height="{tamanho_linha_odt}cm"/> | |
</style:style>""" | |
contents_xml_meio = """</office:automatic-styles> | |
<office:body> | |
<office:text> | |
<text:sequence-decls> | |
<text:sequence-decl text:name="Illustration" text:display-outline-level="0"/> | |
<text:sequence-decl text:name="Table" text:display-outline-level="0"/> | |
<text:sequence-decl text:name="Text" text:display-outline-level="0"/> | |
<text:sequence-decl text:name="Drawing" text:display-outline-level="0"/> | |
</text:sequence-decls> | |
<table:table table:name="Tabela1" table:style-name="Tabela1"> | |
<table:table-column table:style-name="Tabela1.A"/> | |
""" | |
contents_xml_fim = """ | |
</table:table> | |
<text:p text:style-name="Standard"/> | |
</office:text> | |
</office:body> | |
</office:document-content>""" | |
manifest_xml_inicial = """<?xml version="1.0" encoding="UTF-8"?> | |
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2"> | |
<manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.text"/> | |
<manifest:file-entry manifest:full-path="Configurations2/" manifest:media-type="application/vnd.sun.xml.ui.configuration"/> | |
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/> | |
<manifest:file-entry manifest:full-path="manifest.rdf" manifest:media-type="application/rdf+xml"/> | |
<manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/> | |
<manifest:file-entry manifest:full-path="settings.xml" manifest:media-type="text/xml"/> | |
<manifest:file-entry manifest:full-path="Thumbnails/thumbnail.png" manifest:media-type="image/png"/> | |
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/> | |
""" | |
manifest_xml_final = """</manifest:manifest>""" | |
preenche_contents_style = '' | |
preenche_contents_table = '' | |
preenche_manifest = '' | |
zipname = arquivo_odt | |
for c,item in enumerate(lista_arquivos): | |
file_jpg = item[item.rfind("\\")+1:] | |
row = c +1 | |
adiciona_imagens(zipname, file_jpg) | |
base_contents_style = f""" | |
<style:style style:name="Tabela1.A{row}" style:family="table-cell"> | |
<style:table-cell-properties fo:border="none" fo:padding-bottom="0cm" fo:padding-top="0cm" fo:padding-right="0.191cm" fo:padding-left="0.191cm" fo:background-color="transparent"> | |
<style:background-image style:repeat="stretch" xlink:actuate="onLoad" xlink:type="simple" xlink:href="Pictures/{file_jpg}"/> | |
</style:table-cell-properties> | |
</style:style> | |
""" | |
preenche_contents_style += base_contents_style | |
base_contents_table = f"""<table:table-row table:style-name="Tabela1.1"> | |
<table:table-cell table:style-name="Tabela1.A{row}" office:value-type="string"> | |
<text:p text:style-name="Standard"/> | |
</table:table-cell> | |
</table:table-row> | |
""" | |
preenche_contents_table += base_contents_table | |
base_manifest = f''' | |
<manifest:file-entry manifest:media-type="image/png" manifest:full-path="Pictures/{file_jpg}"/>''' | |
preenche_manifest += base_manifest | |
time.sleep(0.5) | |
try:os.remove(item) | |
except: | |
print('Erro apagar') | |
time.sleep(1) | |
os.remove(item) | |
contents_xml_final = (contents_xml_inicial + | |
preenche_contents_style + | |
contents_xml_meio + | |
preenche_contents_table + | |
contents_xml_fim) | |
manifest_xml_final = (manifest_xml_inicial + | |
preenche_manifest + | |
manifest_xml_final) | |
time.sleep(1) | |
update_zip(zipname,'content.xml', contents_xml_final ) | |
time.sleep(1) | |
try: | |
update_zip(zipname,'META-INF/manifest.xml', manifest_xml_final ) | |
except: | |
time.sleep(2) | |
update_zip(zipname,'META-INF/manifest.xml', manifest_xml_final ) | |
print("Encerrado! Arquivo criado:",zipname ) | |
time.sleep(0.5) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment