简体   繁体   中英

Insert picture using python-docx

Using python-docx, is there any way to insert a picture in an existing file? I would need something like "insert_before_paragraph" for inserting pictures. Thanks

Depending on what you want to do, there could be a bold workaround: making docx to docx conversion with pandoc , and putting your own custom filter in the middle, written with panflute .

pandoc sample.docx -o sample2.docx --filter=my_filter.py

Pandoc is a tool that transforms several formats of documents into a standardized Abstract Syntax Tree (AST). Filters operate on the AST. It so happens that pandoc filters can insert prepared images (here are examples of what filters can do ).

It means that you can convert a docx document to html, but also back onto docx... and you have the added benefit that you can get (in case you wish) your output in lots of formats.

In the the process, you will lose the fine points of your formatting (headings, bold and italics should be preserved), but it that's not a problem to you, that would be fine.

This is not exactly the easy way, but once one gets the hang of it, pandoc filters are not bad, and they are a powerful tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM