Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pybarcode ImageWriter和docx模块将条形码图像附加到docx文件_Python - Fatal编程技术网

Python 使用pybarcode ImageWriter和docx模块将条形码图像附加到docx文件

Python 使用pybarcode ImageWriter和docx模块将条形码图像附加到docx文件,python,Python,如何减小pybarcode ImageWriter生成的图像大小,以及如何将多个图像以正确的对齐方式附加到docx文件 我阅读了ImageWriter的dpi选项,但没有了解如何使用它 import barcode from barcode.writer import ImageWriter from docx import * if __name__ == '__main__': # Default set of relationshipships - these a

如何减小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')

通常,barcode.writer不会提供有关生成的输出图像大小的参数,您可以寻求帮助。而且正确的对齐方式对编码来说并不准确,但您可以尝试使用表格将它们放置在正确的位置

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

对于docx文件,一些表格示例是,您可能需要知道什么是适当的aliment,然后键入代码

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