使用pybarcode ImageWriter和docx modu将条形码图像附加到docx文件中

2024-05-19 21:55:53 发布

您现在位置:Python中文网/ 问答频道 /正文

如何减小pybarcode ImageWriter生成的图像大小,以及如何以适当的对齐方式将多个图像附加到docx文件中?在

我读过ImageWriter的dpi选项,但不知道如何使用它。在

import barcode
from barcode.writer import ImageWriter
from docx import *

if __name__ == '__main__':        
    # Default set of relationshipships - these are the minimum components of a document
    ean = barcode.get_barcode('ean', '123456789102', writer=ImageWriter())
    ean.default_writer_options['module_height'] = 3.0
    ean.default_writer_options['module_width'] = 0.1
    filename = ean.save('bar_image')    

    relationships = relationshiplist()

    # Make a new document tree - this is the main part of a Word document
    document = newdocument()

    # This xpath location is where most interesting content lives 
    docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
    # Add an image
    relationships,picpara = picture(relationships, filename,'This is a test description')
    docbody.append(picpara)

    # Create our properties, contenttypes, and other support files
    coreprops = coreproperties(title='Python docx demo',subject='A practical example of making docx from Python',creator='Mike MacCana',keywords=['python','Office Open XML','Word'])
    appprops = appproperties()
    contenttypes = contenttypes()
    websettings = websettings()
    wordrelationships = wordrelationships(relationships)

    # Save our document
        savedocx(document,coreprops,appprops,contenttypes,websettings,wordrelationships,'sample_barcode.docx')

Tags: offrom图像importisdocumenteanbarcode
1条回答
网友
1楼 · 发布于 2024-05-19 21:55:53

一般来说条形码.writer给出生成的输出图像大小的参数,您可以请求PIL帮助。而且正确对齐对于代码来说并不精确,但是您可以尝试使用表来将它们放在正确的位置。在

在PIL中,可以通过以下方式将png图像调整为(480320)

from PIL import Image
im = Image.open("barcode.png")
im.resize((480,320)).save("barcode_resized.png")

对于docx文件,一些表示例是here,您可能需要知道什么是正确的aliment,然后键入代码。在

相关问题 更多 >