Knowledgebase (2015)
Typically, the content of a PDF document needs to follow a logical flow, such as a report is usually structured with chapters, sections, and subsections. When the pages within a PDF are not arranged in the correct sequence, the coherence of the document will be affected. By reordering the pages, you can ensure that the information is presented in a clear and understandable manner. In this article, you will learn how to reorder the pages in a PDF file with Python 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 VS Code 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 in VS Code
Reorder PDF Pages with Python
Spire.PDF for Python provides the PdfDocument.Pages.ReArrange(orderArray: List[int]) method to rearrange the pages in a PDF file. The parameter orderArray is a list of integers which allows you to reorder the PDF pages by specifying the page index in the desired order.
The following are the detailed steps to rearrange the PDF page order with Python:
- Create a PdfDocument instance.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Rearrange the page order of the PDF file using PdfDocument.Pages.ReArrange(orderArray: List[int]) method.
- Save the result file using PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import * from spire.pdf import * inputFile = "Report.pdf" outputFile = "RearrangePDFPageOrder.pdf" # Create a PdfDocument instance pdf = PdfDocument() # Load a PDF file pdf.LoadFromFile(inputFile) # Reorder pages in the PDF file pdf.Pages.ReArrange([3, 2, 0, 1]) # Save the result file pdf.SaveToFile(outputFile, FileFormat.PDF) pdf.Close()
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.
Python: Set the Background Color and Image for Excel Worksheets
2024-03-15 08:01:56 Written by support iceblueFor data analysis and reporting, visual aesthetics play a significant role in presenting information effectively. When working with Excel worksheets, the ability to set background colors and images enhances the overall readability and impact of the data. By leveraging the power of Python, developers can effortlessly manipulate Excel files and customize the appearance of their worksheets. This article demonstrates how to use Spire.XLS for Python to set the background color and image for Excel worksheets with Python programs.
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 VS Code through the following pip command.
pip install Spire.XLS
If you are unsure how to install, please refer to: How to Install Spire.XLS for Python in VS Code
Set the Background Color for an Excel Worksheet
With Spire.XLS for Python, developers can set the background color for a specified cell range through CellRange.Style.Color property. The detailed steps for setting the background color for the used cell range in a worksheet are as follows:
- Create an instance of Workbook class.
- Load an Excel workbook using Workbook.LoadFromFile() method.
- Get a worksheet using Workbook.Worksheets.get_Item() method.
- Get the used range in the worksheet through Worksheet.AllocatedRange property.
- Set the background color of the used range through CellRange.Style.Color property.
- Save the workbook using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create an instance of Workbook class wb = Workbook() # Load an Excel file wb.LoadFromFile("Sample.xlsx") # Get a worksheet sheet = wb.Worksheets.get_Item(0) # Get the used range of the worksheet usedRange = sheet.AllocatedRange # Set the background color of the used range to a light and soft color usedRange.Style.Color = Color.FromRgb(144, 238, 144) # Save the workbook wb.SaveToFile("output/ExcelBackgroundColor.xlsx", FileFormat.Version2016) wb.Dispose()
Set the Background Image for an Excel Worksheet
Setting a background image for an Excel worksheet can be accomplished through PageSetup class. Using the Worksheet.PageSetup.BackgroundImage property, developers can set the image background for the entire worksheet. Detailed steps are as follows:
- Create an instance of Workbook class.
- Load an Excel workbook using Workbook.LoadFromFile() method.
- Get a worksheet using Workbook.Worksheets.get_Item() method.
- Load an image using Image.FromFile() method.
- Set the background image of the worksheet through Worksheet.PageSetup.BackgroundImage property.
- Save the workbook using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create an instance of Workbook class wb = Workbook() # Load an Excel file wb.LoadFromFile("Sample.xlsx") # Get a worksheet sheet = wb.Worksheets.get_Item(0) # Load an image image = Image.FromFile("BackgroundImage.jpg") # Set the background of the worksheet sheet.PageSetup.BackgoundImage = image # Save the workbook wb.SaveToFile("output/ExcelBackgroundImage.xlsx", FileFormat.Version2016) wb.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.
Shapes are a powerful tool in Excel that enables you to transform raw data into visually appealing and informative representations. By inserting and customizing shapes, you can create clear, engaging, and visually impactful spreadsheets that effectively communicate your data and captivate your audience. In this article, we will demonstrate how to insert and remove shapes 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 VS Code 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 in VS Code
Insert Shapes in Excel in Python
You can add numerous types of shapes, such as lines, rectangles, triangles, and stars, to an Excel worksheet by using the Worksheet.PrstGeomShapes.AddPrstGeomShape() method provided by Spire.XLS for Python. Once added, you can customize the shapes, such as adding text to the shapes, filling the shapes with solid or gradient colors or images, and setting shadow styles for the shapes. The detailed steps are as follows.
- Create an object of the Workbook class.
- Get the first worksheet using Workbook.Worksheets[] property.
- Add a shape to the worksheet using Worksheet.PrstGeomShapes.AddPrstGeomShape() method.
- Add text to the shape using IPrstGeomShape.Text property.
- Fill the shape with a color using IPrstGeomShape.Fill.ForeColor property.
- Set the fill type of the shape as solid using IPrstGeomShape.Fill.FillType property.
- Repeat the above steps to add more shapes to the worksheet.
- Save the result file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Get the first worksheet sheet = workbook.Worksheets[0] # Add a triangle shape to the worksheet triangle = sheet.PrstGeomShapes.AddPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle) # Add text to the shape triangle.Text = "Text" # Fill the triangle with a solid color triangle.Fill.ForeColor = Color.get_Yellow() triangle.Fill.FillType = ShapeFillType.SolidColor # Add a heart shape to the worksheet heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart) # Fill the heart with a gradient color heart.Fill.ForeColor = Color.get_Red() heart.Fill.FillType = ShapeFillType.Gradient # Add an arrow shape with the default color to the worksheet arrow = sheet.PrstGeomShapes.AddPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow) # Set shadow style for the arrow arrow.Shadow.Angle = 90 arrow.Shadow.Distance = 10 arrow.Shadow.Size = 150 arrow.Shadow.Color = Color.get_Gray() arrow.Shadow.Blur = 30 arrow.Shadow.Transparency = 1 arrow.Shadow.HasCustomStyle = True # Add a cloud shape to the worksheet cloud = sheet.PrstGeomShapes.AddPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud) # Fill the cloud with a custom picture cloud.Fill.CustomPicture(Image.FromFile("Hydrangea.jpg"), "Hydrangea.jpg") cloud.Fill.FillType = ShapeFillType.Picture # Save the result file workbook.SaveToFile("InsertShapes.xlsx", ExcelVersion.Version2013) workbook.Dispose()
Remove Shapes from Excel in Python
Shapes can improve the visual appearance of your workbook, but they can also increase the file size of it. Removing unnecessary shapes helps reduce the file size, making it more manageable and easier to share or store. Spire.XLS for Python enables you to remove specific shapes from a worksheet effortlessly by using the Worksheet.PrstGeomShapes[index].Remove() method. The detailed steps are as follows.
- Create an object of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Remove a specific shape from the Worksheet using Worksheet.PrstGeomShapes[index].Remove() method.
- Save the result file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * # Create a Workbook object workbook = Workbook() # Load an Excel file workbook.LoadFromFile("InsertShapes.xlsx") # Get the first worksheet sheet = workbook.Worksheets[0] # Remove the first shape from the worksheet sheet.PrstGeomShapes[0].Remove() #Save to file. workbook.SaveToFile("RemoveShapes.xlsx", ExcelVersion.Version2013) workbook.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.