We're pleased to announce the release of Spire.PDF 11.6.14. This version enhances the conversions from PDF to Excel/ Images/ Markdown, and also successfully fixes some known issues that occurred when setting fonts and rendering Arabic text. More details are listed below.

Here is a list of all changes made in this release

Category ID Description
Bug SPIREPDF-4390
SPIREPDF-4462
Fixes the issue that the Arabic text rendering effect was displayed incorrectly.
Bug SPIREPDF-6586 Fixes the issue where the content was incorrect when converting PDF to Excel.
Bug SPIREPDF-7177 Fixes the issue where the bold text styles were lost when converting PDF to Markdown.
Bug SPIREPDF-7381 Fixes the issue where the output was incorrect when converting PDF to Image.
Bug SPIREPDF-7428 Fixes the issue where black background images were generated when converting PDF to Image.
Bug SPIREPDF-7498 Fixes the issue where font size did not auto adjust when setting fonts and "Autosize" for PDF text fields.
Bug SPIREPDF-7522
SPIREPDF-7537
Fixed the issue that it was failed to remove JavaScript from a PDF document.
Click the link to download Spire.PDF 11.6.14:
More information of Spire.PDF new release or hotfix:

We are excited to announce the release of Spire.XLS 15.6.6. The latest version supports the SHEETS function. Besides, some known bugs are fixed successfully in this version, such as the issue that the AutoFitColumns effect was not working correctly. More details are listed below.

Here is a list of changes made in this release

Category ID Description
New feature SPIREXLS-5806 Supports the SHEETS function.
sheet.Range["B2"].Formula = "=SHEETS()";
sheet.Range["B3"].Formula = "=SHEETS(Sheet2!C5)";
sheet.Range["B4"].Formula = "=SHEETS(Sheet3:Sheet5!A1)";
sheet.Range["B6"].Formula = "=SHEETS(Sheet4!C5);
Bug SPIREXLS-5802 Fixes the issue where the AutoFitColumns effect was not working correctly.
Bug SPIREXLS-5804 Fixes the issue where the style of shapes was incorrect when copying worksheets.
Bug SPIREXLS-5814
SPIREXLS-5853
Fixes the issue where the data was incorrect when saving a sheet as an image.
Bug SPIREXLS-5819 Fixes the issue where the saved Excel file reported errors when opened.
Bug SPIREXLS-5832 Fixes the issue where saving an Excel file to PDF/A3B was incorrect.
Bug SPIREXLS-5834 Fixes the issue where the left and right margins were incorrect when converting Excel to PDF.
Bug SPIREXLS-5841 Fixes the issue where the content was incorrect when converting a CellRange to an image.
Click the link to download Spire.XLS 15.6.6:
More information of Spire.XLS new release or hotfix:

Read PDF files in Python using Spire.PDF library

Reading PDF files using Python is essential for tasks like document automation, content analysis, and data scraping. Whether you're working with contracts, reports, invoices, or scientific papers, being able to programmatically access PDF content saves time and enables powerful workflows.

To reliably read PDF content in Python — including text, tables, images, and metadata — you need a reliable Python PDF reader. In this guide, we’ll show you how to read PDFs in Python using Spire.PDF for Python, a professional and easy-to-use library that supports full-featured PDF reading without relying on any third-party tools.

Here's what's covered:


Environment Setup for Reading PDFs in Python

Spire.PDF for Python is a powerful Python PDF reader that allows users to read PDF content with simple Python code, including text, tables, images, and metadata. It offers a developer-friendly interface and supports a wide range of PDF reading operations:

  • Read PDF files from disk or memory
  • Access text, tables, metadata, and images
  • No need for third-party tools
  • High accuracy for structured data reading
  • Free version available

It’s suitable for developers who want to read and process PDFs with minimal setup.

You can install Spire.PDF for Python via pip:

pip install spire.pdf

Or the free version Free Spire.PDF for Python for small tasks:

pip install spire.pdf.free

Load a PDF File in Python

Before accessing content, the first step is to load the PDF into memory. Spire.PDF lets you read PDF files from a path on disk or directly from in-memory byte streams — ideal for reading from web uploads or APIs.

Read PDF from File Path

To begin reading a PDF in Python, load the file using PdfDocument.LoadFromFile(). This creates a document object you can use to access content.

from spire.pdf import PdfDocument

# Create a PdfDocument instance
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("sample.pdf")

Read PDF from Bytes (In-Memory)

To read a PDF file from memory without saving it to disk, you can first load its byte content and then initialize a PdfDocument using a Stream object. This method is especially useful when handling PDF files received from web uploads, APIs, or temporary in-memory data.

from spire.pdf import PdfDocument, Stream

# Read the PDF file to a byte array
with open("sample.pdf", "rb") as f:
    byte_data = f.read()
    
# Create a stream using the byte array
pdfStream = Stream(byte_data)
# Create a PdfDocument using the stream
pdf = PdfDocument(pdfStream)

To go further, check out this guide: Loading and Saving PDFs via Byte Streams in Python


Read Text from PDF Pages in Python

Reading text from a PDF file is one of the most common use cases in document automation. With Spire.PDF, you can easily retrieve all visible text from the entire PDF or from individual pages using simple methods.

Read All Text from PDF

To extract all text from a PDF, loop through each page and call PdfTextExtractor.ExtractText() to collect visible text content.

from spire.pdf import PdfDocument, PdfTextExtractor, PdfTextExtractOptions

# Create a PdfDocument instance
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("sample.pdf")

all_text = ""
# Loop through each page
for pageIndex in range(pdf.Pages.Count):
    page = pdf.Pages.get_Item(pageIndex)
    # Create a PdfTextExtract instance
    text_extractor = PdfTextExtractor(page)
    # Configure extracting options
    options = PdfTextExtractOptions()
    options.IsExtractAllText = True
    options.IsSimpleExtraction = True
    # Extract text from the current page
    all_text += text_extractor.ExtractText(options)
print(all_text)

Sample text content retrieved:

Python code to extract all text from a PDF using Spire.PDF

Read Text from Specific Area of a Page

You can also read text from a defined region of a page using a bounding box. This is useful when only a portion of the layout contains relevant information.

from spire.pdf import RectangleF, PdfDocument, PdfTextExtractor, PdfTextExtractOptions

# Load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("sample.pdf")

# Get the first page
page = pdf.Pages.get_Item(0)
# Create a PdfTextExtractor instance
textExtractor = PdfTextExtractor(page)
# Set the area to extract text by configuring the PdfTextExtractOptions
options = PdfTextExtractOptions()
area = RectangleF.FromLTRB(0, 200, page.Size.Width, 270)  # x, y, width, height
options.ExtractArea = area
options.IsSimpleExtraction = True

# Extract text from the area
text = textExtractor.ExtractText(options)
print(text)

The text read from the PDF page area:

Python example of reading text from a defined area in a PDF


Read Table Data from PDFs in Python

PDF tables are often used in reports, invoices, and statements. With Spire.PDF, you can read PDF tables in Python by extracting structured tabular content using its layout-aware table extractor, making it ideal for financial and business documents. Use PdfTableExtractor.ExtractTable() to detect tables page by page and output each row and cell as structured text.

from spire.pdf import PdfDocument, PdfTableExtractor

# Load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("sample.pdf")

# Create a PdfTableExtractor instance
table_extractor = PdfTableExtractor(pdf)
# Extract the table from the first page
tables = table_extractor.ExtractTable(0)
for table in tables:
    # Get the number of rows and columns
    row_count = table.GetRowCount()
    column_count = table.GetColumnCount()
    # Iterate all rows
    for i in range(row_count):
        table_row = []
        # Iterate all columns
        for j in range(column_count):
            # Get the cell
            cell_text = table.GetText(i, j)
            table_row.append(cell_text)
        print(table_row)

Table content extracted using the code above:

Read table data from a PDF in Python using Spire.PDF

Want to extract text from scanned PDFs using OCR? Read this guide on OCR with Python


Read Images from PDF in Python

PDF files often contain logos, scanned pages, or embedded images. Spire.PDF allows you to read and export these images, which is helpful for working with digitized documents or preserving visual content. Use PdfImageHelper.GetImagesInfo() on each page to retrieve and save all embedded images.

from spire.pdf import PdfDocument, PdfImageHelper

# Load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("sample.pdf")

# Get the first page
page = pdf.Pages.get_Item(0)

# Create a PdfImageHelper object
image_helper = PdfImageHelper()
# Get the image information from the page
images_info = image_helper.GetImagesInfo(page)
# Save the images from the page as image files
for i in range(len(images_info)):
    images_info[i].Image.Save("output/Images/image" + str(i) + ".png")

The image read from the PDF file:

Extract images from PDF pages using Spire.PDF and Python


Read PDF Metadata (Title, Author, etc.)

Sometimes you may want to access document metadata like author, subject, and title. This can be helpful for indexing or organizing files. Use the ocumentInformation property to read metadata fields.

from spire.pdf import PdfDocument

# Load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("sample.pdf")

# Get the document properties
properties = pdf.DocumentInformation
print("Title: " + properties.Title)
print("Author: " + properties.Author)
print("Subject: " + properties.Subject)
print("Keywords: " + properties.Keywords)

The metadata read from the PDF document:

Get PDF metadata such as author and title using Python


Common Questions on Reading PDFs

Can Python parse a PDF file?

Yes. Libraries like Spire.PDF for Python allow you to read PDF text, extract tables, and access embedded images or metadata. It supports methods like PdfTextExtractor.ExtractText() and PdfTableExtractor.ExtractTable() for structured content parsing.

How do I read a PDF in Jupyter?

Spire.PDF works seamlessly in Jupyter Notebooks. Just install it via pip and use its API to read PDF files, extract text, or parse tables and images directly in your notebook environment.

How to read text from a PDF file?

Use the PdfTextExtractor.ExtractText() method on each page after loading the PDF with Spire.PDF. This lets you read PDF file to text in Python and retrieve visible content for processing or analysis.

Can I read a PDF file without saving it to disk?

Yes. You can use LoadFromStream() to read PDF content as bytes and load it directly from memory. This is useful for processing PDFs received from web APIs or file uploads.


Conclusion

With Spire.PDF for Python, you can easily read a PDF in Python — including reading PDF text, tables, images, and metadata — and even read a PDF file to text for further processing or automation. This makes it an ideal solution for document automation, data ingestion, and content parsing in Python.

Need to process large PDF files or unlock all features? Request a free license and take full advantage of Spire.PDF for Python today!

While working with CSV files is common in data processing, Excel (XLSX) often provides more advantages when it comes to data sharing, visualization, and large-scale analysis. In this guide, you’ll learn how to convert CSV to Excel in Python, including both single file and batch conversion methods. Whether you're automating reports or preparing data for further analysis, this guide will help you handle the conversion efficiently.

Convert CSV to Excel in Python Guide

Why Convert CSV to Excel?

While CSV files are widely used for data storage and exchange due to their simplicity, they come with several limitations—especially when it comes to formatting, presentation, and usability. Converting CSV to Excel can bring several advantages:

Benefits of Converting CSV to Excel

  • Better formatting support: Excel allows rich formatting options like fonts, colors, borders, and cell merging, making your data easier to read and present.
  • Multiple worksheets: Unlike CSV files that support only a single sheet, Excel files can store multiple worksheets in one file, which is better for large datasets.
  • Built-in formulas and charts: You can apply Excel formulas, pivot tables, and charts to analyze and visualize your data.
  • Improved compatibility for business users: Excel is the preferred tool for many non-technical users, making it easier to share and collaborate on data.

Limitations of CSV Files

  • No styling or formatting (plain text only)
  • Single-sheet structure only
  • Encoding issues (e.g., with non-English characters)
  • Not ideal for large datasets or advanced reporting If your workflow involves reporting, data analysis, or sharing data with others, converting CSV to Excel is often a more practical and flexible choice.

Install Required Python Libraries

This guide demonstrates how to effortlessly convert CSV to Excel using Spire.XLS for Python. Spire.XLS is a powerful and professional Python Excel library that allows you to read, edit, and convert Excel files (both .xlsx and .xls) without relying on Microsoft Excel. Installing this CSV to Excel converter on your device is simple — just run the following command:

pip install Spire.XLS

Alternatively, you can download the Spire.XLS package manually for custom installation.

How to Convert CSV to Excel in Python: Single File

Now let’s get to the main part — how to convert a single CSV file to Excel using Python. With the help of Spire.XLS, this task becomes incredibly simple. All it takes is three easy steps: create a new workbook, load the CSV file, and save it as an Excel (.xlsx) file. Below is a detailed walkthrough along with a complete code example — let’s take a look!

Steps to convert a single CSV to Excel in Python:

  • Create a Workbook instance.
  • Load a sample CSV file using Workbook.LoadFromFile() method.
  • Save the CSV file as Excel through Workbook.SaveToFile() method.

Below is the Python code to convert a CSV file to Excel. It also ignores parsing errors and automatically adjusts the column widths for better readability.

from spire.xls import *
from spire.xls.common import *

# Create a workbook
workbook = Workbook()

# Load a csv file
workbook.LoadFromFile("/sample csv.csv", ",", 1, 1)
  
# Set ignore error options
sheet = workbook.Worksheets[0]
sheet.Range["D2:E19"].IgnoreErrorOptions = IgnoreErrorType.NumberAsText
sheet.AllocatedRange.AutoFitColumns()  

# Save the document and launch it
workbook.SaveToFile("/CSVToExcel1.xlsx", ExcelVersion.Version2013)

Convert Single CSV to Excel in Python

Warm Note: If you're only working with small files or doing some light testing, you can also use the free Spire.XLS. It's a great option for getting started quickly.

How to Batch Convert CSV to XLSX in Python

Another common scenario is when you need to convert multiple CSV files to Excel. Instead of manually replacing the file path and name for each one, there's a much more efficient approach. Simply place all the CSV files in the same folder, then use Python to loop through each file and convert them to Excel using the Workbook.SaveToFile() method. Let’s walk through the detailed steps below!

Steps to batch convert CSVs to Excel files in Python:

  • Specify the file paths of input and output folders.
  • Loop through all CSV files in the input folder.
  • Create an object of Workbook class.
  • Load each CSV file from the input folder with Workbook.LoadFromFile() method.
  • Save the current CSV as an Excel file through Workbook.SaveToFile() method.

Here's the Python code to batch convert CSV to Excel (.XLSX):

import os
from spire.xls import *

input_folder = r"E:input\New folder"
output_folder = r"output\New folder"

# Loop through each CSV file
for csv_file in os.listdir(input_folder):
    if csv_file.endswith(".csv"):
        input_path = os.path.join(input_folder, csv_file)
        output_name = os.path.splitext(csv_file)[0] + ".xlsx"
        output_path = os.path.join(output_folder, output_name)

        # Create a Workbook instance and load CSV files
        workbook = Workbook()
        workbook.LoadFromFile(input_path, ",", 1, 1)

        # Save each CSV file as an Excel file
        workbook.SaveToFile(output_path, ExcelVersion.Version2013)

 Batch Convert CSV Files to Excel Files in Python

The Conclusion

This guide showed you how to convert CSV to Excel in Python with step-by-step instructions and complete code examples. Whether you're working with a single CSV file or multiple files, Spire.XLS makes the process simple, fast, and hassle-free. Need help with more advanced scenarios or other Excel-related tasks? Feel free to contact us anytime!

FAQs about Converting CSV to Excel

Q1: How to convert CSV to Excel in Python without pandas?
A: You can use libraries like Spire.XLS, openpyxl, or xlsxwriter to convert CSV files without relying on pandas. These tools provide simple APIs to load .csv files and export them as xlsx—no Microsoft Excel installation required.

Q2: What is the easiest way to convert multiple CSV files to Excel in Python?
A: Just place all CSV files in one folder, then loop through them in Python and convert each using Workbook.SaveToFile(). This approach is ideal for batch processing. Alternatively, online converters can be a quick fix for occasional use.

Q3: How to auto-adjust column width when converting CSV to Excel in Python?
A: After loading the CSV, call worksheet.autoFitColumns() in Spire.XLS to automatically resize columns based on content before saving the Excel file.

We are excited to announce the release of Spire.Presentation for Java 10.6.0. The issue where the height of the shape decreased after adding blank paragraphs is fixed in the new version. More details are listed below.

Here is a list of changes made in this release

Category ID Description
Bug SPIREPPT-2860 Fixes the issue where the height of the shape decreased after adding blank paragraphs.
Click the link below to download Spire.Presentation for Java 10.6.0:
Friday, 30 May 2025 10:09

Spire.PDFViewer 8.1.3 fixes an issue

We are delighted to announce the release of Spire.PDFViewer 8.1.3. This version fixes the issue that the control threw “NullReferenceException” after setting the “Anchor” property. More details are listed below.

Here is a list of changes made in this release

Category ID Description
Bug SPIREPDFVIEWER-606 Fixes the issue that the control threw “NullReferenceException” after setting the “Anchor” property.
Click the link to get Spire.PDFViewer 8.1.3:
More information of Spire.PDFViewer new release or hotfix:

Excel charts are excellent tools for presenting data clearly and effectively. However, when you need to use them outside of Excel — such as in presentations, reports, or websites — it becomes necessary to save Excel charts as images. Exporting a chart to formats like JPG, PNG, or BMP makes it easier to share, reuse, and embed across various platforms. Whether you're preparing business reports, publishing data visualizations online, or building slide decks, converting Excel charts into image files ensures better compatibility and presentation control.

Illustration of saving Excel chart as image using manual, VBA, and C# methods

In this guide, you’ll learn three effective ways to convert Excel charts to image formats like JPG or PNG:

Whether you're looking to do a quick one-off export or batch-process reports with code, this article will help you pick the best approach.


Save a Chart in Excel as an Image (Manual Method)

The simplest method to save chart in Excel as image is to do it manually:

  1. Open the Excel file in Microsoft Excel and select the chart in your Excel worksheet.

  2. Right-click and choose "Save as Picture...".

Right-click context menu on Excel chart showing Save as Picture option

  1. Choose the file format (PNG, JPG, BMP) and save location.

Save As dialog in Excel to export chart as JPG or PNG image

Exported Chart Image:

Result of manually saving Excel chart as PNG image in Excel interface

This method is perfect for quickly turning a chart into a picture for emails or documents. It works in all modern Excel versions and supports various output formats — making it ideal when you want to save an Excel graph as an image without writing any code.

You may also like: Convert Excel Worksheets to Images Online


Convert Excel Chart to Image in C# with Spire.XLS

If you need to automate the export of Excel charts to images, especially in enterprise or reporting systems, using C# with Spire.XLS for .NET is the most scalable solution.

Install Spire.XLS for .NET

Install via NuGet:

PM> Install-Package Spire.XLS

Or use Free Spire.XLS for .NET for smaller tasks:

PM> Install-Package FreeSpire.XLS

C# Sample Code: Save Excel Chart to JPG

using Spire.Xls;
using System.Drawing;

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

foreach (Worksheet sheet in workbook.Worksheets)
{
    foreach (Chart chart in sheet.Charts)
    {
        Bitmap chartImage = chart.SaveToImage();
        chartImage.Save($"output/{sheet.Name} - {chart.ChartTitle}.jpg");
    }
}
workbook.Dispose();

Result:

Excel chart exported to image using C# and Spire.XLS with chart.SaveToImage method

This method supports JPG, PNG, BMP, and more. You can control the output by simply changing the file extension in the Save() method — Bitmap will automatically infer the image format based on it.


Save Excel Chart as Image via VBA Macro

If you're working within Excel and want a semi-automated approach, you can use VBA to export charts as images.

How to Insert VBA Code into Excel

  1. Press Alt + F11 to open the Visual Basic for Applications editor.
  2. In the left panel, right-click on VBAProject (YourWorkbookName.xlsm) and choose Insert > Module.

Insert a new VBA module to export Excel chart as image

  1. Paste the macro code below into the module.

Paste VBA code for saving Excel chart as JPG or PNG

  1. Save the file as Excel Macro-Enabled Workbook (.xlsm).

Save the Excel file as Excel Macro-Enabled XLSM file

  1. Press F5 in the the Visual Basic for Applications editor to run, or link it to a button using Excel’s Form Controls.

Run VBA macro to save Excel chart as image from chart object

VBA Macro Code Example

Sub SaveChartAsImage()
    Dim chartObj As ChartObject
    Dim folderPath As String
    Dim savePath As String

    folderPath = ThisWorkbook.Path & "\Charts"
    If Dir(folderPath, vbDirectory) = "" Then MkDir folderPath

    savePath = folderPath & "\MyChart.jpg"
    Set chartObj = ActiveSheet.ChartObjects(1)
    chartObj.Chart.Export Filename:=savePath, FilterName:="JPG"
End Sub

Exported Chart Image:

Exported Excel chart as JPG using VBA macro and chart export function

This macro is ideal for users who frequently work in Excel and need to export charts with minimal effort.


Comparison: Manual vs C# vs VBA Methods

Method Best For Format Support Automation Level
Manual Quick one-time exports PNG, JPG, BMP ❌ None
C# + Spire.XLS Batch, reporting, enterprise use JPG, PNG, BMP, etc ✅ Full
VBA Macro Repetitive tasks in Excel UI JPG, PNG ⚠️ Limited

Which Should You Choose?

  • Use manual saving for occasional image needs.
  • Use VBA if you work heavily inside Excel and prefer macro buttons.
  • Use C# and Spire.XLS if you want full control, automation, and high-quality output.

Conclusion

Saving Excel charts as images is a common need across business, reporting, and data visualization scenarios. This article introduced:

  • How to save Excel chart as image manually
  • How to save Excel chart as image in C# using Spire.XLS
  • How to use Excel VBA to export chart as image

With these methods, you can choose the approach that best fits your workflow and technical skillset.


FAQs

How do I save a chart in Excel as an image manually?

Right-click on the chart, select "Save as Picture...", and choose an image format like PNG or JPG.

How do I save Excel chart as image using code?

Use Spire.XLS with C# and call chart.SaveToImage() to export to image formats like JPG or PNG.

Check out the Spire.XLS for .NET tutorial to learn more about C# techniques for working with Excel charts.

Can I automate saving charts from Excel?

Yes. Use either C# for full automation or VBA macros within Excel for semi-automated workflows.

Does Bitmap in C# support JPG and PNG?

Yes. The Bitmap.Save() method detects the format based on the file extension, supporting formats like JPG, PNG, BMP, and more.

See Also

Tuesday, 09 January 2024 08:11

Crie arquivos PDF com Python

PDF (Portable Document Format) é um formato de arquivo popular amplamente utilizado para gerar documentos jurídicos, contratos, relatórios, faturas, manuais, e-books e muito mais. Fornece um formato versátil e confiável para compartilhar, armazenar e apresentar documentos eletrônicos de maneira consistente, independente de qualquer software, hardware ou sistema operacional.

Dadas estas vantagens, a geração automatizada de documentos PDF está se tornando cada vez mais importante em vários campos. Para automatizar o processo de criação de PDF em Python, você pode escrever scripts que geram PDFs com base em requisitos específicos ou dados de entrada. Este artigo fornecerá exemplos detalhados para demonstrar como usar Python para criar arquivos PDF programaticamente.

Biblioteca geradora de PDF em Python

Para gerar PDF usando Python, precisaremos usar a biblioteca Spire.PDF for Python. É uma biblioteca Python poderosa que fornece recursos de geração e processamento de PDF. Com ele, podemos usar python para criar PDFs do zero e adicionar vários elementos PDF às páginas PDF.

Para instalar a biblioteca do gerador de PDF Python, basta usar o seguinte comando pip para instalar a partir do PyPI:

pip install Spire.PDF

Conhecimento prévio

Antes de começar, vamos aprender algumas informações básicas sobre como criar um arquivo PDF usando a biblioteca Spire.PDF for Python.

Página PDF: Uma página no Spire.PDF for Python é representada pela classe PdfPageBase, que consiste em uma área do cliente e margens ao redor. A área de conteúdo serve para os usuários escreverem vários conteúdos, e as margens geralmente são bordas em branco.

Sistema de Coordenadas: Conforme mostrado na figura abaixo, a origem do sistema de coordenadas na página está localizada no canto superior esquerdo da área do cliente, com o eixo x estendendo-se horizontalmente para a direita e o eixo y estendendo-se verticalmente para baixo. Todos os elementos adicionados à área do cliente são baseados nas coordenadas X e Y especificadas.

Create PDF Files with Python

Classes e métodos: a tabela a seguir lista algumas das principais classes e métodos usados para criar PDFs em Python.

Membro Descrição
Classe PDFDocument Representa um modelo de documento PDF.
Classe PDFPageBase Representa uma página em um documento PDF.
Classe PDFSolidBrush Representa um pincel que preenche qualquer objeto com uma cor sólida.
Classe PDFTrueTypeFont Representa uma fonte True Type.
Classe PDFStringFormat Representa informações de formato de texto, como alinhamento, espaçamento de caracteres e recuo.
Classe PDFTextWidget Representa a área de texto com capacidade de abranger várias páginas.
Classe PDFTextLayout Representa as informações de layout de texto.
Método PdfDocument.Pages.Add() Adiciona uma página a um documento PDF.
Método PdfPageBase.Canvas.DrawString() Desenha uma string no local especificado em uma página com fonte e objetos de pincel especificados.
Método PdfPageBase.Canvas.DrawImage() Desenha uma imagem em um local especificado em uma página.
Método PdfTextWidget.Draw() Desenha o widget de texto no local especificado em uma página.
Método PdfDocument.SaveToFile() Salva o documento em um arquivo PDF.

Como criar PDF usando Python

A seguir estão as etapas principais para criar arquivos PDF em Python:

  • Instale Spire.PDF for Python.
  • Importe módulos.
  • Crie um documento PDF por meio da classe PdfDocument.
  • Adicione uma página ao PDF usando o método PdfDocument.Pages.Add() e retorne um objeto da classe PdfPageBase.
  • Crie o pincel e a fonte do PDF desejados.
  • Desenhe uma string de texto ou widget de texto em uma coordenada especificada na página PDF usando o método PdfPageBase.Canvas.DrawString() ou PdfTextWidget.Draw().
  • Salve o documento PDF usando o método PdfDocument.SaveToFile().

Python para criar arquivos PDF do zero

O exemplo de código a seguir demonstra como usar Python para criar um arquivo PDF e inserir texto e imagens. Com o Spire.PDF for Python, você também pode inserir outros elementos PDF, como listas, hiperlinks, formulários e carimbos.

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a pdf document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add()

# Specify title text and paragraph content
titleText = "Spire.PDF for Python"
paraText = "Spire.PDF for Python is a professional PDF development component that enables developers to create, read, edit, convert, and save PDF files in Python programs without depending on any external applications or libraries. This Python PDF class library provides developers with various functions to create PDF files from scratch or process existing PDF documents completely through Python programs."

# Create solid brushes
titleBrush = PdfSolidBrush(PdfRGBColor(Color.get_Blue()))
paraBrush = PdfSolidBrush(PdfRGBColor(Color.get_Black()))

# Create fonts
titleFont = PdfFont(PdfFontFamily.Helvetica, 14.0, PdfFontStyle.Bold)
paraFont = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Regular, True)

# Set the text alignment
textAlignment = PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

# Draw title on the page
page.Canvas.DrawString(titleText, titleFont, titleBrush, page.Canvas.ClientSize.Width / 2, 40.0, textAlignment)

# Create a PdfTextWidget object to hold the paragraph content
textWidget = PdfTextWidget(paraText, paraFont, paraBrush)

# Create a rectangle where the paragraph content will be placed
rect = RectangleF(PointF(0.0, 50.0), page.Canvas.ClientSize)

# Set the text layout
textLayout = PdfTextLayout()
textLayout.Layout = PdfLayoutType.Paginate

# Draw the widget on the page
textWidget.Draw(page, rect, textLayout)

# Load an image
image = PdfImage.FromFile("Python.png")

# Draw the image at a specified location on the page
page.Canvas.DrawImage(image, 12.0, 130.0)

#Save the PDF document
pdf.SaveToFile("CreatePDF.pdf")
pdf.Close()

Create PDF Files with Python

Python para gerar PDF a partir de arquivo de texto

O exemplo de código a seguir mostra o processo de leitura de texto de um arquivo .txt e desenhá-lo em um local especificado em uma página PDF.

  • Python
from spire.pdf.common import *
from spire.pdf import *

def ReadFromTxt(fname: str) -> str:
    with open(fname, 'r') as f:
        text = f.read()
    return text

# Create a pdf document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add(PdfPageSize.A4(), PdfMargins(20.0, 20.0))

# Create a PdfFont and brush
font = PdfFont(PdfFontFamily.TimesRoman, 12.0)
brush = PdfBrushes.get_Black()

# Get content from a .txt file
text = ReadFromTxt("text.txt")

# Create a PdfTextWidget object to hold the text content
textWidget = PdfTextWidget(text, font, brush)

# Create a rectangle where the text content will be placed
rect = RectangleF(PointF(0.0, 50.0), page.Canvas.ClientSize)

# Set the text layout
textLayout = PdfTextLayout()
textLayout.Layout = PdfLayoutType.Paginate

# Draw the widget on the page
textWidget.Draw(page, rect, textLayout)

# Save the generated PDF file
pdf.SaveToFile("GeneratePdfFromText.pdf", FileFormat.PDF)
pdf.Close()

Create PDF Files with Python

Python para criar um PDF com várias colunas

PDFs com várias colunas são comumente usados em revistas ou jornais. O exemplo de código a seguir mostra o processo de criação de um PDF de duas colunas desenhando texto em duas áreas retangulares separadas em uma página PDF.

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Creates a PDF document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add()

# Define paragraph text
s1 = "Databases allow access to various services which, in turn, allow you to access your accounts and perform transactions all across the internet. " + "For example, your bank's login page will ping a database to figure out if you've entered the right password and username. " + "Your favorite online shop pings your credit card's database to pull down the funds needed to buy that item you've been eyeing."
s2 = "Databases make research and data analysis much easier because they are highly structured storage areas of data and information. " + "This means businesses and organizations can easily analyze databases once they know how a database is structured. " + "Common structures and common database querying languages (e.g., SQL) make database analysis easy and efficient."

# Get width and height of page
pageWidth = page.GetClientSize().Width
pageHeight = page.GetClientSize().Height

# Create a PDF font and brush
font = PdfFont(PdfFontFamily.TimesRoman, 12.0)
brush = PdfBrushes.get_Black()

# Set the text alignment
format = PdfStringFormat(PdfTextAlignment.Left)

# Draws text at a specified location on the page
page.Canvas.DrawString(s1, font, brush, RectangleF(10.0, 20.0, pageWidth / 2 - 8, pageHeight), format)
page.Canvas.DrawString(s2, font, brush, RectangleF(pageWidth / 2 + 8, 20.0, pageWidth / 2 - 8, pageHeight), format)

# Save the PDF document
pdf.SaveToFile("CreateTwoColumnPDF.pdf")
pdf.Close()

Create PDF Files with Python

Licença gratuita para criação de PDF em Python

Você pode obtenha uma licença temporária gratuita do Spire.PDF for Python para gerar documentos PDF sem marcas d'água e limitações.

Conclusão

Esta postagem do blog forneceu um guia passo a passo sobre como criar arquivos PDF com base no sistema de coordenadas definido na biblioteca Spire.PDF for Python. Nos exemplos de código, você pode aprender sobre o processo e métodos de inserção de texto, imagens em PDFs e conversão de arquivos TXT em PDFs. Se quiser explorar outros recursos de processamento e conversão de PDF da biblioteca Python PDF, você pode verificar sua documentação online.

Para qualquer problema durante o uso, entre em contato com nossa equipe de suporte técnico por e-mail ou fórum.

Veja também

PDF (Portable Document Format) — популярный формат файлов, широко используемый для создания юридических документов, контрактов, отчетов, счетов-фактур, руководств, электронных книг и многого другого. Он обеспечивает универсальный и надежный формат для единообразного обмена, хранения и представления электронных документов, независимо от какого-либо программного обеспечения, оборудования или операционных систем.

Учитывая эти преимущества, автоматизированное создание PDF-документов становится все более важным в различных областях. Чтобы автоматизировать процесс создания PDF-файлов на Python, вы можете написать сценарии, генерирующие PDF-файлы на основе определенных требований или входных данных. В этой статье будут приведены подробные примеры, демонстрирующие, как использовать Python для создания PDF-файлов программно.

Библиотека PDF-генератора Python

Чтобы создать PDF-файл с помощью Python, нам понадобится библиотека Spire.PDF for Python. Это мощная библиотека Python, обеспечивающая возможности создания и обработки PDF-файлов. С его помощью мы можем использовать Python для создания PDF-файлов с нуля и добавления различных элементов PDF на страницы PDF.

Чтобы установить библиотеку генератора PDF Python, просто используйте следующую команду pip для установки из PyPI:

pip install Spire.PDF

Жизненный опыт

Прежде чем мы начнем, давайте изучим некоторые сведения о создании PDF-файла с использованием библиотеки Spire.PDF for Python.

Страница PDF: страница в Spire.PDF for Python представлена классом PdfPageBase, который состоит из клиентской области и полей вокруг. Область содержимого предназначена для того, чтобы пользователи могли писать различное содержимое, а поля обычно представляют собой пустые края.

Система координат: Как показано на рисунке ниже, начало системы координат на странице расположено в верхнем левом углу клиентской области, при этом ось X проходит горизонтально вправо, а ось Y — вертикально вниз. Все элементы, добавляемые в клиентскую область, основаны на указанных координатах X и Y.

Create PDF Files with Python

Классы и методы: В следующей таблице перечислены некоторые основные классы и методы, используемые для создания PDF-файлов в Python.

Член Описание
Класс PDFDocument Представляет модель документа PDF.
Класс PDFPageBase Представляет страницу в PDF-документе.
Класс PdfSolidBrush Представляет кисть, которая закрашивает любой объект сплошным цветом.
Класс PdfTrueTypeFont Представляет шрифт истинного типа.
Класс PdfStringFormat Представляет информацию о текстовом формате, такую как выравнивание, интервал между символами и отступ.
Класс PdfTextWidget Представляет текстовую область, способную занимать несколько страниц.
Класс PdfTextLayout Представляет информацию о макете текста.
Метод PDFDocument.Pages.Add() Добавляет страницу в PDF-документ.
Метод PdfPageBase.Canvas.DrawString() Рисует строку в указанном месте на странице с указанными объектами шрифта и кисти.
Метод PdfPageBase.Canvas.DrawImage() Рисует изображение в указанном месте на странице.
Метод PdfTextWidget.Draw() Рисует текстовый виджет в указанном месте на странице.
Метод PDFDocument.SaveToFile() Сохраняет документ в PDF-файл.

Как создать PDF с помощью Python

Ниже приведены основные шаги по созданию PDF-файлов в Python:

  • Установите Spire.PDF for Python.
  • Импортируйте модули.
  • Создайте PDF-документ с помощью класса PdfDocument.
  • Добавьте страницу в PDF с помощью метода PdfDocument.Pages.Add() и верните объект класса PdfPageBase.
  • Создайте нужную кисть и шрифт PDF.
  • Нарисуйте текстовую строку или текстовый виджет по заданной координате на странице PDF с помощью метода PdfPageBase.Canvas.DrawString() или PdfTextWidget.Draw().
  • Сохраните PDF-документ с помощью метода PdfDocument.SaveToFile().

Python для создания PDF-файлов с нуля

В следующем примере кода показано, как использовать Python для создания файла PDF и вставки текста и изображений. С помощью Spire.PDF for Python вы также можете вставлять другие элементы PDF, например списки, гиперссылки, формы и штампы.

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a pdf document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add()

# Specify title text and paragraph content
titleText = "Spire.PDF for Python"
paraText = "Spire.PDF for Python is a professional PDF development component that enables developers to create, read, edit, convert, and save PDF files in Python programs without depending on any external applications or libraries. This Python PDF class library provides developers with various functions to create PDF files from scratch or process existing PDF documents completely through Python programs."

# Create solid brushes
titleBrush = PdfSolidBrush(PdfRGBColor(Color.get_Blue()))
paraBrush = PdfSolidBrush(PdfRGBColor(Color.get_Black()))

# Create fonts
titleFont = PdfFont(PdfFontFamily.Helvetica, 14.0, PdfFontStyle.Bold)
paraFont = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Regular, True)

# Set the text alignment
textAlignment = PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

# Draw title on the page
page.Canvas.DrawString(titleText, titleFont, titleBrush, page.Canvas.ClientSize.Width / 2, 40.0, textAlignment)

# Create a PdfTextWidget object to hold the paragraph content
textWidget = PdfTextWidget(paraText, paraFont, paraBrush)

# Create a rectangle where the paragraph content will be placed
rect = RectangleF(PointF(0.0, 50.0), page.Canvas.ClientSize)

# Set the text layout
textLayout = PdfTextLayout()
textLayout.Layout = PdfLayoutType.Paginate

# Draw the widget on the page
textWidget.Draw(page, rect, textLayout)

# Load an image
image = PdfImage.FromFile("Python.png")

# Draw the image at a specified location on the page
page.Canvas.DrawImage(image, 12.0, 130.0)

#Save the PDF document
pdf.SaveToFile("CreatePDF.pdf")
pdf.Close()

Create PDF Files with Python

Python для создания PDF из текстового файла

В следующем примере кода показан процесс чтения текста из файла .txt и его рисования в указанном месте на странице PDF.

  • Python
from spire.pdf.common import *
from spire.pdf import *

def ReadFromTxt(fname: str) -> str:
    with open(fname, 'r') as f:
        text = f.read()
    return text

# Create a pdf document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add(PdfPageSize.A4(), PdfMargins(20.0, 20.0))

# Create a PdfFont and brush
font = PdfFont(PdfFontFamily.TimesRoman, 12.0)
brush = PdfBrushes.get_Black()

# Get content from a .txt file
text = ReadFromTxt("text.txt")

# Create a PdfTextWidget object to hold the text content
textWidget = PdfTextWidget(text, font, brush)

# Create a rectangle where the text content will be placed
rect = RectangleF(PointF(0.0, 50.0), page.Canvas.ClientSize)

# Set the text layout
textLayout = PdfTextLayout()
textLayout.Layout = PdfLayoutType.Paginate

# Draw the widget on the page
textWidget.Draw(page, rect, textLayout)

# Save the generated PDF file
pdf.SaveToFile("GeneratePdfFromText.pdf", FileFormat.PDF)
pdf.Close()

Create PDF Files with Python

Python для создания многоколоночного PDF-файла

Многоколоночный PDF-файл обычно используется в журналах и газетах. В следующем примере кода показан процесс создания PDF-файла с двумя столбцами путем рисования текста в двух отдельных прямоугольных областях на странице PDF.

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Creates a PDF document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add()

# Define paragraph text
s1 = "Databases allow access to various services which, in turn, allow you to access your accounts and perform transactions all across the internet. " + "For example, your bank's login page will ping a database to figure out if you've entered the right password and username. " + "Your favorite online shop pings your credit card's database to pull down the funds needed to buy that item you've been eyeing."
s2 = "Databases make research and data analysis much easier because they are highly structured storage areas of data and information. " + "This means businesses and organizations can easily analyze databases once they know how a database is structured. " + "Common structures and common database querying languages (e.g., SQL) make database analysis easy and efficient."

# Get width and height of page
pageWidth = page.GetClientSize().Width
pageHeight = page.GetClientSize().Height

# Create a PDF font and brush
font = PdfFont(PdfFontFamily.TimesRoman, 12.0)
brush = PdfBrushes.get_Black()

# Set the text alignment
format = PdfStringFormat(PdfTextAlignment.Left)

# Draws text at a specified location on the page
page.Canvas.DrawString(s1, font, brush, RectangleF(10.0, 20.0, pageWidth / 2 - 8, pageHeight), format)
page.Canvas.DrawString(s2, font, brush, RectangleF(pageWidth / 2 + 8, 20.0, pageWidth / 2 - 8, pageHeight), format)

# Save the PDF document
pdf.SaveToFile("CreateTwoColumnPDF.pdf")
pdf.Close()

Create PDF Files with Python

Бесплатная лицензия для создания PDF-файлов на Python

Ты можешь получить бесплатную временную лицензию Spire.PDF for Python для создания PDF-документов без водяных знаков и ограничений.

Заключение

В этой записи блога представлено пошаговое руководство по созданию файлов PDF на основе системы координат, определенной в библиотеке Spire.PDF for Python. В примерах кода вы можете узнать о процессе и методах вставки текста, изображений в PDF-файлы и преобразования файлов TXT в PDF-файлы. Если вы хотите изучить другие функции обработки и преобразования PDF-файлов библиотеки Python PDF, вы можете ознакомиться с ее онлайн-документация.

По любым вопросам во время использования обращайтесь в нашу службу технической поддержки по электронной почте или на форуме.

Смотрите также

Tuesday, 09 January 2024 08:09

Erstellen Sie PDF-Dateien mit Python

PDF (Portable Document Format) ist ein beliebtes Dateiformat, das häufig zum Erstellen von Rechtsdokumenten, Verträgen, Berichten, Rechnungen, Handbüchern, E-Books und mehr verwendet wird. Es bietet ein vielseitiges und zuverlässiges Format zum einheitlichen Teilen, Speichern und Präsentieren elektronischer Dokumente, unabhängig von Software, Hardware oder Betriebssystemen.

Aufgrund dieser Vorteile gewinnt die automatisierte Generierung von PDF-Dokumenten in verschiedenen Bereichen zunehmend an Bedeutung. Um den PDF-Erstellungsprozess in Python zu automatisieren, können Sie Skripte schreiben, die PDFs basierend auf bestimmten Anforderungen oder Eingabedaten generieren. Dieser Artikel enthält detaillierte Beispiele, um die Verwendung zu veranschaulichen Python zum Erstellen von PDF-Dateien programmatisch.

Python PDF Generator-Bibliothek

Um PDFs mit Python zu generieren, müssen wir die Spire.PDF for Python-Bibliothek verwenden. Es handelt sich um eine leistungsstarke Python-Bibliothek, die Funktionen zur PDF-Generierung und -Verarbeitung bietet. Damit können wir mit Python PDFs von Grund auf erstellen und verschiedene PDF-Elemente zu PDF-Seiten hinzufügen.

Um die Python-PDF-Generator-Bibliothek zu installieren, verwenden Sie einfach den folgenden pip-Befehl zur Installation von PyPI:

pip install Spire.PDF

Hintergrundwissen

Bevor wir beginnen, lernen wir einige Hintergrundinformationen zum Erstellen einer PDF-Datei mit der Bibliothek Spire.PDF for Python kennen.

PDF-Seite: Eine Seite in Spire.PDF for Python wird durch die Klasse PdfPageBase dargestellt, die aus einem Clientbereich und Rändern rundherum besteht. Der Inhaltsbereich dient dem Benutzer zum Schreiben verschiedener Inhalte, und die Ränder sind normalerweise leere Kanten.

Koordinatensystem: Wie in der Abbildung unten dargestellt, befindet sich der Ursprung des Koordinatensystems auf der Seite in der oberen linken Ecke des Clientbereichs, wobei die x-Achse horizontal nach rechts und die y-Achse vertikal nach unten verläuft. Alle zum Clientbereich hinzugefügten Elemente basieren auf den angegebenen X- und Y-Koordinaten.

Create PDF Files with Python

Klassen und Methoden: Die folgende Tabelle listet einige der Kernklassen und Methoden auf, die zum Erstellen von PDFs in Python verwendet werden.

Mitglied Beschreibung
PDFDocument-Klasse Stellt ein PDF-Dokumentmodell dar.
PDFPageBase-Klasse Stellt eine Seite in einem PDF-Dokument dar.
PdfSolidBrush-Klasse Stellt einen Pinsel dar, der jedes Objekt mit einer Volltonfarbe füllt.
PdfTrueTypeFont-Klasse Stellt eine True-Type-Schriftart dar.
PdfStringFormat-Klasse Stellt Informationen zum Textformat dar, z. B. Ausrichtung, Zeichenabstand und Einzug.
PDFTextWidget-Klasse Stellt den Textbereich dar, der sich über mehrere Seiten erstrecken kann.
PDFTextLayout-Klasse Stellt die Textlayoutinformationen dar.
PdfDocument.Pages.Add()-Methode Fügt eine Seite zu einem PDF-Dokument hinzu.
PdfPageBase.Canvas.DrawString()-Methode Zeichnet eine Zeichenfolge an der angegebenen Stelle auf einer Seite mit angegebenen Schriftart- und Pinselobjekten.
PdfPageBase.Canvas.DrawImage()-Methode Zeichnet ein Bild an einer angegebenen Stelle auf einer Seite.
PDFTextWidget.Draw()-Methode Zeichnet das Text-Widget an der angegebenen Stelle auf einer Seite.
PdfDocument.SaveToFile()-Methode Speichert das Dokument in einer PDF-Datei.

So erstellen Sie PDFs mit Python

Im Folgenden sind die Hauptschritte zum Erstellen von PDF-Dateien in Python aufgeführt:

  • Installieren Sie Spire.PDF for Python.
  • Module importieren.
  • Erstellen Sie ein PDF-Dokument über die PdfDocument-Klasse.
  • Fügen Sie mit der Methode PdfDocument.Pages.Add() eine Seite zum PDF hinzu und geben Sie ein Objekt der Klasse PdfPageBase zurück.
  • Erstellen Sie den gewünschten PDF-Pinsel und die gewünschte Schriftart.
  • Zeichnen Sie eine Textzeichenfolge oder ein Text-Widget an einer angegebenen Koordinate auf der PDF-Seite mit der Methode PdfPageBase.Canvas.DrawString() oder PdfTextWidget.Draw().
  • Speichern Sie das PDF-Dokument mit der Methode PdfDocument.SaveToFile().

Python zum Erstellen von PDF-Dateien von Grund auf

Das folgende Codebeispiel zeigt, wie Sie mit Python eine PDF-Datei erstellen und Text und Bilder einfügen. Mit Spire.PDF for Python können Sie auch andere PDF-Elemente einfügen, z Listen, Hyperlinks, Formulare und Stempel.

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a pdf document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add()

# Specify title text and paragraph content
titleText = "Spire.PDF for Python"
paraText = "Spire.PDF for Python is a professional PDF development component that enables developers to create, read, edit, convert, and save PDF files in Python programs without depending on any external applications or libraries. This Python PDF class library provides developers with various functions to create PDF files from scratch or process existing PDF documents completely through Python programs."

# Create solid brushes
titleBrush = PdfSolidBrush(PdfRGBColor(Color.get_Blue()))
paraBrush = PdfSolidBrush(PdfRGBColor(Color.get_Black()))

# Create fonts
titleFont = PdfFont(PdfFontFamily.Helvetica, 14.0, PdfFontStyle.Bold)
paraFont = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Regular, True)

# Set the text alignment
textAlignment = PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

# Draw title on the page
page.Canvas.DrawString(titleText, titleFont, titleBrush, page.Canvas.ClientSize.Width / 2, 40.0, textAlignment)

# Create a PdfTextWidget object to hold the paragraph content
textWidget = PdfTextWidget(paraText, paraFont, paraBrush)

# Create a rectangle where the paragraph content will be placed
rect = RectangleF(PointF(0.0, 50.0), page.Canvas.ClientSize)

# Set the text layout
textLayout = PdfTextLayout()
textLayout.Layout = PdfLayoutType.Paginate

# Draw the widget on the page
textWidget.Draw(page, rect, textLayout)

# Load an image
image = PdfImage.FromFile("Python.png")

# Draw the image at a specified location on the page
page.Canvas.DrawImage(image, 12.0, 130.0)

#Save the PDF document
pdf.SaveToFile("CreatePDF.pdf")
pdf.Close()

Create PDF Files with Python

Python zum Generieren von PDF aus einer Textdatei

Das folgende Codebeispiel zeigt den Prozess des Lesens von Text aus einer TXT-Datei und dessen Zeichnen an einer bestimmten Stelle auf einer PDF-Seite.

  • Python
from spire.pdf.common import *
from spire.pdf import *

def ReadFromTxt(fname: str) -> str:
    with open(fname, 'r') as f:
        text = f.read()
    return text

# Create a pdf document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add(PdfPageSize.A4(), PdfMargins(20.0, 20.0))

# Create a PdfFont and brush
font = PdfFont(PdfFontFamily.TimesRoman, 12.0)
brush = PdfBrushes.get_Black()

# Get content from a .txt file
text = ReadFromTxt("text.txt")

# Create a PdfTextWidget object to hold the text content
textWidget = PdfTextWidget(text, font, brush)

# Create a rectangle where the text content will be placed
rect = RectangleF(PointF(0.0, 50.0), page.Canvas.ClientSize)

# Set the text layout
textLayout = PdfTextLayout()
textLayout.Layout = PdfLayoutType.Paginate

# Draw the widget on the page
textWidget.Draw(page, rect, textLayout)

# Save the generated PDF file
pdf.SaveToFile("GeneratePdfFromText.pdf", FileFormat.PDF)
pdf.Close()

Create PDF Files with Python

Python zum Erstellen eines mehrspaltigen PDF

Mehrspaltige PDFs werden häufig in Zeitschriften oder Zeitungen verwendet. Das folgende Codebeispiel zeigt den Prozess der Erstellung einer zweispaltigen PDF-Datei durch Zeichnen von Text in zwei separaten rechteckigen Bereichen auf einer PDF-Seite.

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Creates a PDF document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add()

# Define paragraph text
s1 = "Databases allow access to various services which, in turn, allow you to access your accounts and perform transactions all across the internet. " + "For example, your bank's login page will ping a database to figure out if you've entered the right password and username. " + "Your favorite online shop pings your credit card's database to pull down the funds needed to buy that item you've been eyeing."
s2 = "Databases make research and data analysis much easier because they are highly structured storage areas of data and information. " + "This means businesses and organizations can easily analyze databases once they know how a database is structured. " + "Common structures and common database querying languages (e.g., SQL) make database analysis easy and efficient."

# Get width and height of page
pageWidth = page.GetClientSize().Width
pageHeight = page.GetClientSize().Height

# Create a PDF font and brush
font = PdfFont(PdfFontFamily.TimesRoman, 12.0)
brush = PdfBrushes.get_Black()

# Set the text alignment
format = PdfStringFormat(PdfTextAlignment.Left)

# Draws text at a specified location on the page
page.Canvas.DrawString(s1, font, brush, RectangleF(10.0, 20.0, pageWidth / 2 - 8, pageHeight), format)
page.Canvas.DrawString(s2, font, brush, RectangleF(pageWidth / 2 + 8, 20.0, pageWidth / 2 - 8, pageHeight), format)

# Save the PDF document
pdf.SaveToFile("CreateTwoColumnPDF.pdf")
pdf.Close()

Create PDF Files with Python

Kostenlose Lizenz zum Erstellen von PDFs in Python

Du kannst Holen Sie sich eine kostenlose temporäre Lizenz von Spire.PDF for Python zum Generieren von PDF-Dokumenten ohne Wasserzeichen und Einschränkungen.

Abschluss

In diesem Blogbeitrag finden Sie eine Schritt-für-Schritt-Anleitung zum Erstellen von PDF-Dateien basierend auf dem in der Spire.PDF for Python-Bibliothek definierten Koordinatensystem. In den Codebeispielen erfahren Sie mehr über den Prozess und die Methoden zum Einfügen von Text und Bildern in PDFs und zum Konvertieren von TXT-Dateien in PDFs. Wenn Sie andere PDF-Verarbeitungs- und Konvertierungsfunktionen der Python PDF-Bibliothek erkunden möchten, können Sie sich diese ansehen Online-Dokumentation.

Bei Problemen während der Nutzung wenden Sie sich bitte per E-Mail oder Forum an unser technisches Support-Team. E-Mail oder Forum.

Siehe auch