We are excited to announce the release of Spire.Doc 12.5.5. This version supports preserving FormField and SDT data and setting the comment display mode when converting Word to PDF. Besides, it enhances the conversion from Word to HTML and HTML to Word. Moreover, some known issues are fixed successfully in this version, such as the issue that the program threw “ArgumentOutOfRangeException” exception when loading Word documents.

Here is a list of changes made in this release

Category ID Description
New feature - Supports preserving FormField (CheckBox, DropDown, TextFormField) and SDT (CheckBox, Text, RichText, DropDownList, ComboBox) data when converting Word to PDF.
ToPdfParameterList.PreserveFormFields = true;
New feature - Supports setting three display modes for comments (Hide, ShowInBalloons, ShowInAnnotations) when converting Word to PDF.
Document.LayoutOptions.CommentDisplayMode = CommentDisplayMode.ShowInAnnotations;
Bug SPIREDOC-10152 Fixes the issue that the table widths and fonts are not correct when converting HTML to Word.
Bug SPIREDOC-10363 Fixes the issue that the program threw "ArgumentOutOfRangeException" when loading Word documents.
Bug SPIREDOC-10371 Fixes the issue that headings were missing after converting Word to HTML.
Bug SPIREDOC-10376 Fixes the issue that table borders were missing after converting Word to HTML.
Bug SPIREDOC-10402 Fixes the issue that tables were missing when converting Word to HTML.
Bug SPIREDOC-10421 Fixes the issue that the program threw "InvalidCastException" exception when comparing Word documents.
Bug SPIREDOC-10427 Fixes the issue that the header was lost after adding a watermark to Word and saving it to PDF.
Click the link to download Spire.Doc 12.5.5:
More information of Spire.Doc new release or hotfix:

Document properties provide additional information about an Excel file, such as author, title, subject, and other metadata associated with the file. Retrieving these properties from Excel can help users gain insight into the file content and history, enabling better organization and management of files. At times, users may also need to remove document properties to protect the privacy and confidentiality of the information contained in the file. In this article, you will learn how to read or remove document properties in Excel in Python using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Read Standard and Custom Document Properties in Excel in Python

Excel properties are divided into two main categories:

  • Standard Properties: These are predefined properties that are built into Excel files. They typically include basic details about the file such as title, subject, author, keywords, etc.
  • Custom Properties: These are user-defined attributes that can be added to Excel to track additional information about the file based on your specific needs.

Spire.XLS for Python allows to read both the standard and custom document properties of an Excel file. The following are the detailed steps:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Create a StringBuilder instance.
  • Get a collection of all standard document properties using Workbook.DocumentProperties property.
  • Get specific standard document properties using the properties of the BuiltInDocumentProperties class and append them to the StringBuilder instance.
  • Get a collection of all custom document properties using Workbook.CustomDocumentProperties property.
  • Iterate through the collection.
  • Get the name, type, and value of each custom document property using ICustomDocumentProperties[].Name, ICustomDocumentProperties[].PropertyType and ICustomDocumentProperties[].Value properties.
  • Determine the specific property type, and then convert the property value to the value of the corresponding data type.
  • Append the property name and converted property value to the StringBuilder instance using StringBuilde.append() method.
  • Write the content of the StringBuilder instance into a txt file.
  • Python
from spire.xls import *
from spire.xls.common import *

def AppendAllText(fname: str, text: List[str]):
    fp = open(fname, "w")
    for s in text:
        fp.write(s + "\n")
    fp.close()

inputFile = "Budget Template.xlsx"
outputFile = "GetExcelProperties.txt"

# Create a Workbook instance
workbook = Workbook()

# Load an Excel document from disk
workbook.LoadFromFile(inputFile)

# Create a StringBuilder instance
builder = []

# Get a collection of all standard document properties
standardProperties = workbook.DocumentProperties

# Get specific standard properties and append them to the StringBuilder instance
builder.append("Standard Document Properties:")
builder.append("Title: " + standardProperties.Title)
builder.append("Subject: " + standardProperties.Subject)
builder.append("Category: " + standardProperties.Category)
builder.append("Keywords: " + standardProperties.Keywords)
builder.append("Comments: " + standardProperties.Comments)
builder.append("")

# Get a collection of all custom document properties
customProperties = workbook.CustomDocumentProperties

builder.append("Custom Properties:")
# Iterate through the collection
for i in range(len(customProperties)):
    
    # Get the name, type, and value of each custom document property
    name = customProperties[i].Name
    type = customProperties[i].PropertyType
    obj = customProperties[i].Value

    # Determine the specific property type, and then convert the property value to the value of the corresponding data type
    value = None
    if type == PropertyType.Double:
        value = Double(obj).Value
    elif type == PropertyType.DateTime:
        value = DateTime(obj).ToShortDateString()
    elif type == PropertyType.Bool:
        value = Boolean(obj).Value
    elif type == PropertyType.Int:
        value = Int32(obj).Value
    elif type == PropertyType.Int32:
        value = Int32(obj).Value
    else:
        value = String(obj).Value

    # Append the property name and converted property value to the StringBuilder instance   
    builder.append(name + ": " + str(value))

# Write the content of the StringBuilder instance into a text file
AppendAllText(outputFile, builder)
workbook.Dispose()

Python: Read or Remove Document Properties in Excel

Remove Standard and Custom Document Properties in Excel in Python

You can easily delete standard document properties from an Excel file by setting their values as empty. For custom document properties, you can use the ICustomDocumentProperties.Remove() method to delete them. The following are the detailed steps:

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a collection of all standard document properties using Workbook.DocumentProperties property.
  • Set the values of specific standard document properties as empty through the corresponding properties of the BuiltInDocumentProperties class.
  • Get a collection of all custom document properties using Workbook.CustomDocumentProperties property.
  • Iterate through the collection.
  • Delete each custom property from the collection by its name using ICustomDocumentProperties.Remove() method.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "Budget Template.xlsx"
outputFile = "RemoveExcelProperties.xlsx"

# Create a Workbook instance
workbook = Workbook()

# Load an Excel document from disk
workbook.LoadFromFile(inputFile)

# Get a collection of all standard document properties
standardProperties = workbook.DocumentProperties

# Set the value of each standard document property as empty
standardProperties.Title = ""
standardProperties.Subject = ""
standardProperties.Category = ""
standardProperties.Keywords = ""
standardProperties.Comments = ""

# Get a collection of all custom document properties
customProperties = workbook.CustomDocumentProperties

# Iterate through the collection
for i in range(len(customProperties) - 1, -1, -1):
    # Delete each custom document property from the collection by its name
    customProperties.Remove(customProperties[i].Name)

# Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2016)
workbook.Dispose()

Python: Read or Remove Document Properties in Excel

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.

Proper alignment of tables and text in Microsoft Word is crucial for creating visually appealing and easy-to-read documents. By aligning table headers, numeric data, and text appropriately, you can enhance the organization and clarity of your information, making it more accessible to your readers. In this article, we will demonstrate how to align tables and the text in table cells in Microsoft Word in Python using Spire.Doc for Python.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Align Tables in Word in Python

A table in a Word document can be aligned to the left, center, or right side by using the Table.TableFormat.HorizontalAlignment property. The detailed steps are as follows.

  • Create an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document using Document.Sections[index] property.
  • Get a specific table in the section using Section.Tables[index] property.
  • Set the alignment for the table using Table.TableFormat.HorizontalAlignment property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an instance of the Document class
document = Document()
# Load a Word document
document.LoadFromFile("Tables.docx")

# Get the first section in the document
section = document.Sections[0]

# Get the first, second, and third tables in the section
table1 = section.Tables[0]
table2 = section.Tables[1]
table3 = section.Tables[2]

# Align the first table to the left
table1.TableFormat.HorizontalAlignment = RowAlignment.Left
# Align the second table to the center
table2.TableFormat.HorizontalAlignment = RowAlignment.Center
# Align the third table to the right
table3.TableFormat.HorizontalAlignment = RowAlignment.Right

# Save the result document
document.SaveToFile("AlignTable.docx", FileFormat.Docx2013)
document.Close()

Python: Set Alignment for Table and Table Text in Word

Align the Text in Table Cells in Word in Python

The text within a table cell can be horizontally aligned to the left, center, or right side using the TableCell.Paragraphs[index].Format.HorizontalAlignment property. Additionally, they can also be vertically aligned to the top, center, or bottom of the cell using the TableCell.CellFormat.VerticalAlignment property. The detailed steps are as follows.

  • Create an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document using Document.Sections[index] property.
  • Get a specific table in the section using Section.Tables[index] property.
  • Loop through the rows in the table.
  • Loop through the cells in each row.
  • Set the vertical alignment for the text in each cell using TableCell.CellFormat.VerticalAlignment property.
  • Loop through the paragraphs in each cell.
  • Set the horizontal alignment for each paragraph using TableCell.Paragraphs[index].Format.HorizontalAlignment property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an instance of the Document class
document = Document()
# Load a Word document
document.LoadFromFile("Table.docx")

# Get the first section in the document
section = document.Sections[0]

# Get the first tables in the section
table = section.Tables[0]

# Loop through the rows in the table
for row_index in range(table.Rows.Count):
    row = table.Rows[row_index]
    # Loop through the cells in the row
    for cell_Index in range(row.Cells.Count):
        cell = row.Cells[cell_Index]
        # Vertically align the text in the cell to the center
        cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
        # Horizontally align the text in the cell to the center
        for para_index in range(cell.Paragraphs.Count):
            paragraph = cell.Paragraphs[para_index]
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center

# Save the result document
document.SaveToFile("AlignTableText.docx", FileFormat.Docx2013)
document.Close()

Python: Set Alignment for Table and Table Text in Word

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.

We're pleased to announce the release of Spire.XLS 14.5.3. This version supports adding images to the first page header and footer, obtaining active selection range, as well as finding cells based on regular expressions. In addition, some known issues that occurred when converting Excel to PDF/ images, importing data into Excel, and autofitting columns have been successfully fixed. More details are shown below.

Here is a list of changes made in this release

Category ID Description
New feature SPIREXLS-5128 Supports adding images to the first page header and footer.
//Load image
System.Drawing.Image bufferedImage = System.Drawing.Image.FromFile(inputFile_Img);

//Set image on first page header and footer
wb.Worksheets[0].PageSetup.FirstLeftHeaderImage = bufferedImage;
wb.Worksheets[0].PageSetup.FirstLeftFooterImage = bufferedImage;
wb.Worksheets[1].PageSetup.FirstCenterHeaderImage = bufferedImage;
wb.Worksheets[1].PageSetup.FirstCenterFooterImage = bufferedImage;
wb.Worksheets[2].PageSetup.FirstRightHeaderImage = bufferedImage;
wb.Worksheets[2].PageSetup.FirstRightFooterImage = bufferedImage;
New feature SPIREXLS-5195 Supports obtaining active selection range.
Worksheet worksheet = workbook.Worksheets[0];
string Information = null;
foreach (CellRange range in worksheet.ActiveSelectionRange)
{
Information += "RangeAddressLocal:"+ range.RangeAddressLocal+"\r\n";
Information += "ColumnCount:" + range.ColumnCount + "\r\n";
Information += "ColumnWidth:" + range.ColumnWidth + "\r\n";
Information += "Column:" + range.Column + "\r\n";
Information += "RowCount:" + range.RowCount+ "\r\n";
Information += "RowHeight:" + range.RowHeight + "\r\n";
Information += "Row:" + range.Row + "\r\n";
}
File.WriteAllText(outputFile_TXT,Information);
New feature SPIREXLS-5200 Supports finding cells based on regular expressions.
CellRange[] ranges = sheet.FindAllString(".*test.", false, false, true);
Bug SPIREXLS-5075 Fixes the issue that the image was lost after converting Excel to image.
Bug SPIREXLS-5151 Fixes the issue that the content in the generated PDF document was lost after converting Excel to PDF on the Kirin system.
Bug SPIREXLS-5186 Fixes the issue that the application threw the "System.NullPointerException" when converting sheet to image.
Bug SPIREXLS-5197 Fixes the issue that the border obtained from merged area was incorrect.
Bug SPIREXLS-5198 Fixes the issue that the text and alternative text obtained from checkboxes were incorrect.
Bug SPIREXLS-5214 Fixes the issue that it failed to set the active cell using the SetActiveCell() method.
Bug SPIREXLS-5216 Fixes the issue that the textboxes added to charts were not displayed.
Bug SPIREXLS-5218 Fixes the issue that the name obtained from checkbox was incorrect.
Bug SPIREXLS-5225 Fixes the issue that the mouse cursor position was incorrect after importing data into Excel using the InsertDataTable() method.
Bug SPIREXLS-5228 Fixes the issue that some graphics and lines were lost after converting Excel document to PDF document.
Bug SPIREXLS-5234 Fixes the issue that it failed to autofit columns using the AutoFitCoumns() method.
Click the link to download Spire.XLS 14.5.3:
More information of Spire.XLS new release or hotfix:
Thursday, 23 May 2024 09:02

Python: Create and Scan QR Codes

QR codes are a type of two-dimensional barcode that can store a variety of information, including URLs, contact details, and even payment information. QR codes have become increasingly popular, allowing for quick and convenient access to digital content, making them a useful tool in our modern, technology-driven world.

In this article, you will learn how to create and scan QR codes in Python using Spire.Barcode for Python.

Get a Free Trial License

The trial version of Spire.Barcode for Python does not support scanning QR code images without a valid license being applied. Additionally, it displays an evaluation message on any QR code images that are generated.

To remove these limitations, you can get a 30-day trial license for free.

Create a QR Code in Python

Spire.Barcode for Python offers the BarcodeSettings class, which enables you to configure the settings for generating a barcode. These settings encompass the barcode type, the data to be encoded, the color, the margins, and the horizontal and vertical resolution.

After you have set up the desired settings, you can create a BarcodeGenerator instance using those configurations. Subsequently, you can invoke the GenerateImage() method of the generator to produce the barcode image.

The following are the steps to create a QR code in Python.

  • Create a BarcodeSettings object.
  • Set the barcode type to QR code using BarcodeSettings.Type property.
  • Set the data of the 2D barcode using BarcodeSettings.Data2D property.
  • Set other attributes of the barcode using the properties under the BarcodeSettings object.
  • Create a BarCodeGenerator object based on the settings.
  • Create a QR code image using BarCodeGenerator.GenerateImage() method.
  • Python
from spire.barcode import *

# Write all bytes to a file
def WriteAllBytes(fname: str, data):
    with open(fname, "wb") as fp:
        fp.write(data)
    fp.close()

# Apply license key
License.SetLicenseKey("license key")

# Create a BarcodeSettings object
barcodeSettings = BarcodeSettings()

# Set the type of barcode to QR code
barcodeSettings.Type = BarCodeType.QRCode

# Set the data for the 2D barcode
barcodeSettings.Data2D = "Hello, World"

# Set margins
barcodeSettings.LeftMargin = 0.2
barcodeSettings.RightMargin = 0.2
barcodeSettings.TopMargin = 0.2
barcodeSettings.BottomMargin = 0.2

# Set the horizontal resolution
barcodeSettings.DpiX = 500

# Set the vertical resolution
barcodeSettings.DpiY = 500

# Set error correction level
barcodeSettings.QRCodeECL = QRCodeECL.M

# Do not display text on barcode
barcodeSettings.ShowText = False
  
# Add a logo at the center of the QR code
barcodeSettings.SetQRCodeLogoImage("C:\\Users\\Administrator\\Desktop\\logo.png")

# Create an instance of BarCodeGenerator with the specified settings
barCodeGenerator = BarCodeGenerator(barcodeSettings)

# Generate the image for the barcode
image = barCodeGenerator.GenerateImage()

# Write the PNG image to disk
WriteAllBytes("output/QRCode.png", image)

Python: Create and Scan QR Codes

Scan a QR Code Image in Python

Spire.Barcode provides the BarcodeScanner class, which is responsible for barcode image recognition. This class offers several methods to extract data from barcodes, including:

  • ScanOneFile(): Scans a single barcode image file and returns the extracted data.
  • ScanFile(): Scans all barcodes present in a specified image file and returns the extracted data.
  • ScanStream(): Scans barcodes from a stream of image data and returns the extracted information.

The following code demonstrates how to scan a QR code image using it.

  • Python
from spire.barcode import *

# Apply license key
License.SetLicenseKey("license key")

# Scan an image file that contains one barcode
result = BarcodeScanner.ScanOneFile("C:\\Users\\Administrator\\Desktop\\QRCode.png")

# Scan an image file that contains multiple barcodes
# results = BarcodeScanner.ScanFile("C:\\Users\\Administrator\\Desktop\\Image.png")

# Print the result
print(result)

Python: Create and Scan QR Codes

See Also

Highlighting important text in your PowerPoint slides can be an effective way to draw your audience's attention and emphasize key points. Whether you are presenting complex information or delivering a persuasive pitch, using text highlighting can make your slides more visually engaging and help your message stand out. In this article, we will demonstrate how to highlight text in a PowerPoint presentation in Python using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python. It can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Highlight Text in PowerPoint Presentation in Python

Spire.Presentation for Python provides a method called IAutoShape.TextFrame.HighLightText(text: str, color: Color, options: TextHighLightingOptions) to highlight specific text within the shapes of a PowerPoint presentation.

Follow the steps below to highlight specified text in your presentation using Spire.Presentation for Python:

  • Create an instance of the Presentation class.
  • Load a PowerPoint presentation using the Presentation.LoadFromFile() method.
  • Create an instance of the TextHighLightingOptions class, and set the text highlighting options such as whole words only and case sensitive through the TextHighLightingOptions.WholeWordsOnly and TextHighLightingOptions.CaseSensitive properties.
  • Loop through the slides in the presentation and the shapes on each slide.
  • Check if the current shape is of IAutoShape type.
  • If the result is true, typecast it to an IAutoShape object.
  • Highlight all matches of specific text in the shape using the IAutoShape.TextFrame.HighLightText(text: str, color: Color, options: TextHighLightingOptions) method.
  • Save the result presentation to a new file using the Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Specify the input and output file paths
input_file = "Example.pptx"
output_file = "HighlightText.pptx"

# Create an instance of the Presentation class
ppt = Presentation()
# Load the PowerPoint presentation
ppt.LoadFromFile(input_file)

# Specify the text to highlight
text_to_highlight = "Spire.Presentation"
# Specify the highlight color
highlight_color = Color.get_Yellow()

# Create an instance of the TextHighLightingOptions class
options = TextHighLightingOptions()
# Set the highlight options (case sensitivity and whole word highlighting)
options.WholeWordsOnly = True
options.CaseSensitive = True

# Loop through the slides in the presentation
for slide in ppt.Slides:
    # Loop through the shapes on each slide
    for shape in slide.Shapes:
            # Check if the shape is of IAutoShape type
            if isinstance (shape, IAutoShape):
                # Typecast the shape to an IAutoShape object
                auto_shape = IAutoShape(shape)
                # Search and highlight specified text within the shape
                auto_shape.TextFrame.HighLightText(text_to_highlight, highlight_color, options)

# Save the result presentation to a new PPTX file
ppt.SaveToFile(output_file, FileFormat.Pptx2013)
ppt.Dispose()

Python: Highlight Text in PowerPoint Presentation

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.

Retrieving the coordinates of text or images within a PDF document can quickly locate specific elements, which is valuable for extracting content from PDFs. This capability also enables adding annotations, marks, or stamps to the desired locations in a PDF, allowing for more advanced document processing and manipulation.

In this article, you will learn how to get coordinates of the specified text or image in a PDF document using Spire.PDF for Python.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

Coordinate System in Spire.PDF

When using Spire.PDF to process an existing PDF document, the origin of the coordinate system is located at the top left corner of the page. The X-axis extends horizontally from the origin to the right, and the Y-axis extends vertically downward from the origin (shown as below).

Python: Get Coordinates of the Specified Text or Image in PDF

Get Coordinates of the Specified Text in PDF in Python

To find the coordinates of a specific piece of text within a PDF document, you must first use the PdfTextFinder.Find() method to locate all instances of the target text on a particular page. Once you have found these instances, you can then access the PdfTextFragment.Positions property to retrieve the precise (X, Y) coordinates for each instance of the text.

The steps to get coordinates of the specified text in PDF are as follows.

  • Create a PdfDocument object.
  • Load a PDF document from a specified path.
  • Get a specific page from the document.
  • Create a PdfTextFinder object.
  • Specify find options through PdfTextFinder.Options property.
  • Search for a string within the page using PdfTextFinder.Find() method.
  • Get a specific instance of the search results.
  • Get X and Y coordinates of the text through PdfTextFragment.Positions[0].X and PdfTextFragment.Positions[0].Y properties.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()

# Load a PDF document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Privacy Policy.pdf")

# Get a specific page
page = doc.Pages[0]

# Create a PdfTextFinder object
textFinder = PdfTextFinder(page)

# Specify find options
findOptions = PdfTextFindOptions()
findOptions.Parameter = TextFindParameter.IgnoreCase
findOptions.Parameter = TextFindParameter.WholeWord
textFinder.Options = findOptions
 
# Search for the string "PRIVACY POLICY" within the page
findResults = textFinder.Find("PRIVACY POLICY") 

# Get the first instance of the results
result = findResults[0]

# Get X/Y coordinates of the found text
x = int(result.Positions[0].X)
y = int(result.Positions[0].Y)
print("The coordinates of the first instance of the found text are:", (x, y))

# Dispose resources
doc.Dispose()

Python: Get Coordinates of the Specified Text or Image in PDF

Get Coordinates of the Specified Image in PDF in Python

Spire.PDF for Python provides the PdfImageHelper class, which allows users to extract image details from a specific page within a PDF file. By doing so, you can leverage the PdfImageInfo.Bounds property to retrieve the (X, Y) coordinates of an individual image.

The steps to get coordinates of the specified image in PDF are as follows.

  • Create a PdfDocument object.
  • Load a PDF document from a specified path.
  • Get a specific page from the document.
  • Create a PdfImageHelper object.
  • Get the image information from the page using PdfImageHelper.GetImagesInfo() method.
  • Get X and Y coordinates of a specific image through PdfImageInfo.Bounds property.
  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
doc = PdfDocument()

# Load a PDF document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Privacy Policy.pdf")

# Get a specific page 
page = doc.Pages[0]

# Create a PdfImageHelper object
imageHelper = PdfImageHelper()

# Get image information from the page
imageInformation = imageHelper.GetImagesInfo(page)

# Get X/Y coordinates of a specific image
x = int(imageInformation[0].Bounds.X)
y = int(imageInformation[0].Bounds.Y)
print("The coordinates of the specified image are:", (x, y))

# Dispose resources
doc.Dispose()

Python: Get Coordinates of the Specified Text or Image in PDF

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.

We are happy to announce the release of Spire.XLS for Java 14.5.1. This version enhances the conversion from Excel to PDF and images. Besides, some known issues are fixed successfully in this version, such as the issue that the XLOOKUP formula was not calculated correctly. More details are listed below.

Here is a list of changes made in this release

Category ID Description
Bug SPIREXLS-5215 Fixes the issue that the program threw "NullPointerException" when converting an Excel document to an HTML document.
Bug SPIREXLS-5219 Fixes the issue that the content of charts was incorrect after converting them to pictures.
Bug SPIREXLS-5221 Fixes the issue that the program threw "NullPointerException" exception when calling calculateAllValue() method to calculate formulas and convert Excel documents to pictures.
Bug SPIREXLS-5222 Fixes the issue that the chart content was incorrect after converting an Excel document to a PDF document.
Bug SPIREXLS-5224 Fixes an issue that the chart style was incorrect after loading and saving an XLSM document.
Bug SPIREXLS-5230 Fixes the issue that the XLOOKUP formula was calculated incorrectly.
Click the link to download Spire.XLS for Java 14.5.1:
Monday, 20 May 2024 01:17

Python: Compress PDF Documents

PDF has become the standard for sharing documents across various platforms and devices. However, large PDF files can be cumbersome to share, especially when dealing with limited storage space or slow internet connections. Compressing PDF files is an essential skill to optimize file size and ensure seamless document distribution. In this article, we will demonstrate how to compress PDF documents in Python using Spire.PDF for Python.

Install Spire.PDF for Python

This scenario requires Spire.PDF for Python. It can be easily installed in your Windows through the following pip command.

pip install Spire.PDF

If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows

Compress PDF Documents in Python

PDF files are often large when they include high-resolution images or embedded fonts. To reduce the size of PDF files, consider compressing the images and fonts contained within them. The following steps explain how to compress PDF documents by compressing fonts and images using Spire.PDF for Python:

  • Create a PdfCompressor object to compress a specified PDF file at a given path.
  • Get the OptimizationOptions object using PdfCompressor.OptimizationOptions property.
  • Enable font compression using OptimizationOptions.SetIsCompressFonts(True) method.
  • Set image quality using OptimizationOptions.SetImageQuality(imageQuality:ImageQuality) method.
  • Enable image resizing using OptimizationOptions.SetResizeImages(True) method.
  • Enable image compression using OptimizationOptions.SetIsCompressImage(True) method.
  • Compress the PDF file and save the compressed version to a new file using PdfCompressor.CompressToFile() method.
  • Python
from spire.pdf.common import *
from spire.pdf import *

 # Create a PdfCompressor object to compress the specified PDF file at the given path
compressor = PdfCompressor("C:/Users/Administrator/Documents/Example.pdf")

# Get the OptimizationOptions object
compression_options = compressor.OptimizationOptions

# Enable font compression
compression_options.SetIsCompressFonts(True)
# Enable font unembedding
# compression_options.SetIsUnembedFonts(True)

# Set image quality
compression_options.SetImageQuality(ImageQuality.Medium)
# Enable image resizing
compression_options.SetResizeImages(True)
# Enable image compression
compression_options.SetIsCompressImage(True)

# Compress the PDF file and save the compressed version to a new file
compressor.CompressToFile("Compressed.pdf")

Python: Compress PDF Documents

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.

We're glad to announce the release of Spire.Presentation 9.5.3. This version adds the ColumnSpacing property to set the spacing between columns, and also fixes some issues that occurred when appending images to presentations and converting PPTX to SVG. Check below for more details.

Here is a list of changes made in this release

Category ID Description
New feature SPIREPPT-2497 Adds the ColumnSpacing property to set the spacing between columns.
//The unit is point
shape.TextFrame.ColumnSpacing = 20.50f;
Bug SPIREPPT-2493 Fixes the issue that the application threw the "System.ArgumentException" when appending images to presentations.
Bug SPIREPPT-2498 Fixes the issue that the shape was in opposite direction after converting PPTX to SVG.
Bug SPIREPPT-2500 Fixes the issue that the gradient color of the shape was incorrect after converting PPTX to SVG.
Click the link to download Spire.Presentation 9.5.3:
More information of Spire.Presentation new release or hotfix:
Page 4 of 230