Converting a document from Word to TIFF can be useful when you need to share the content as an image file, such as for electronic forms, presentations, or publishing. The TIFF format preserves the visual layout and appearance of the document. Conversely, converting a TIFF image to a Word document can be helpful when you want to present information in the Word format.
This article demonstrates how to convert Word to TIFF and TIFF to Word (non-editable) using Python and the Spire.Doc for Python library.
Install the Required Libraries
This situation relies on the combination of Spire.Doc for Python and Pillow (PIL). Spire.Doc is used to read, create and convert Word documents, while the PIL library is used for handling TIFF files and accessing their frames.
The libraries can be easily installed on your device through the following pip commands.
pip install Spire.Doc pip install pillow
Convert Word to TIFF in Python
To convert a Word document into a TIFF image, the initial step is to use the Spire.Doc library to load the Word document and transform the individual pages into image data streams. Then, you can leverage the functionality provided by the PIL to merge these separate image streams into a unified TIFF image.
The following are the steps to convert Word to TIFF using Python.
- Create a Document object.
- Load a Word document from a specified file path.
- Iterate through the pages in the document.
- Convert each page into an image stream using Document.SaveImageToSteams() method.
- Convert the image stream into a PIL image.
- Combine these PIL images into a single TIFF image.
- Python
from spire.doc import * from spire.doc.common import * from PIL import Image from io import BytesIO # Create a Document object doc = Document() # Load a Word document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx") # Create an empty list to store PIL Images images = [] # Iterate through pages in the document for i in range(doc.GetPageCount()): # Convert a specific page to image stream with doc.SaveImageToStreams(i, ImageType.Bitmap) as imageData: # Open a specific image stream as a PIL image img = Image.open(BytesIO(imageData.ToArray())) # Append the PIL image to list images.append(img) # Save the PIL Images as a multi-page TIFF file images[0].save("Output/ToTIFF.tiff", save_all=True, append_images=images[1:]) # Dispose resources doc.Dispose()
Convert TIFF to Word in Python
By utilizing PIL library, you can load a TIFF file and break it down into separate PNG images for each frame. You can then utilize the Spire.Doc library to incorporate these separate PNG files as distinct pages within a Microsoft Word document.
To convert a TIFF image to a Word document using Python, follow these steps.
- Create a Document object.
- Add a section to it and set the page margins to zero.
- Load a TIFF image.
- Iterate though the frames in the TIFF image.
- Get a specific frame, and save it as a PNG file.
- Add a paragraph to the section.
- Append the image file to the paragraph.
- Set the page size to be the same as the image size.
- Save the document to a Word file.
- Python
from spire.doc import * from spire.doc.common import * from PIL import Image import io # Create a Document object doc = Document() # Add a section section = doc.AddSection() # Set margins to 0 section.PageSetup.Margins.All = 0.0 # Load a TIFF image tiff_image = Image.open("C:\\Users\\Administrator\\Desktop\\TIFF.tiff") # Iterate through the frames in it for i in range(tiff_image.n_frames): # Go to the current frame tiff_image.seek(i) # Extract the image of the current frame frame_image = tiff_image.copy() # Save the image to a PNG file frame_image.save(f"temp/output_frame_{i}.png") # Add a paragraph paragraph = section.AddParagraph() # Append image to the paragraph image = paragraph.AppendPicture(f"temp/output_frame_{i}.png") # Get image width and height width = image.Width height = image.Height # Set the page size to be the same as the image size section.PageSetup.PageSize = SizeF(width, height) # Save the document to a Word file doc.SaveToFile("Output/ToWord.docx",FileFormat.Docx2019) # Dispose resources doc.Dispose()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.