Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 18, 2025 11:50
Show Gist options
  • Save aspose-com-gists/19e1cfcc9c135003f76ae25ccb15a719 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/19e1cfcc9c135003f76ae25ccb15a719 to your computer and use it in GitHub Desktop.
Best Python PDF Library for Working with PDFs
import aspose.pdf as ap
# Open an existing PDF
document = ap.Document("CreatePDF.pdf")
# Get the first page
page = document.pages[1] # 1-based indexing
# Add new text to the page
text_fragment = ap.text.TextFragment("This text was added programmatically!")
text_fragment.position = ap.text.Position(100, 700)
text_fragment.text_state.font_size = 12
text_fragment.text_state.font = ap.text.FontRepository.find_font("Times New Roman")
page.paragraphs.add(text_fragment)
# Save the modified document
document.save("AddText.pdf")
import aspose.pdf as ap
# Create a new document
document = ap.Document()
# Add a page
page = document.pages.add()
# Add text to the page
text_fragment = ap.text.TextFragment("Hello, Aspose.PDF for Python!")
text_fragment.position = ap.text.Position(100, 600)
text_fragment.text_state.font_size = 14
text_fragment.text_state.font = ap.text.FontRepository.find_font("Arial")
text_fragment.text_state.foreground_color = ap.Color.blue
# Add the text fragment to the page
page.paragraphs.add(text_fragment)
# Add a table
table = ap.Table()
table.column_widths = "100 100 100"
table.default_cell_border = ap.BorderInfo(ap.BorderSide.ALL, 0.5, ap.Color.black)
table.default_cell_padding = ap.MarginInfo(5, 5, 5, 5)
# Add rows and cells
row = table.rows.add()
cell = row.cells.add("Product")
cell = row.cells.add("Quantity")
cell = row.cells.add("Price")
row = table.rows.add()
cell = row.cells.add("Widget A")
cell = row.cells.add("10")
cell = row.cells.add("$5.99")
row = table.rows.add()
cell = row.cells.add("Widget B")
cell = row.cells.add("5")
cell = row.cells.add("$10.99")
# Add the table to the page
page.paragraphs.add(table)
# Save the document
document.save("CreatePDF.pdf")
import aspose.pdf as ap
# Open PDF document
document = ap.Document("AddText.pdf")
textAbsorber = ap.text.TextAbsorber()
document.pages.accept(textAbsorber)
extractedText = textAbsorber.text
# Show the output
print(extractedText)
import aspose.pdf as ap
# Open an existing PDF
document = ap.Document("CreatePDF.pdf")
# Get the first page
page = document.pages[1] # 1-based indexing
# Insert an image
image = ap.Image()
image.file = "aspose-logo.png"
image.fix_width = 400
image.fix_height = 100
page.paragraphs.add(image)
# Save the modified document
document.save("InsertImage.pdf")
import aspose.pdf as ap
input_pdf = DIR_INPUT + "sample.pdf"
output_pdf = DIR_OUTPUT + "convert_pdf_to_xlsx.xlsx"
# Open PDF document
document = ap.Document(input_pdf)
# Create save options
save_option = ap.ExcelSaveOptions()
# Save the file into XLSX
document.save(output_pdf, save_option)
import aspose.pdf as ap
input_pdf = DIR_INPUT + "sample.pdf"
output_pdf = DIR_OUTPUT + "pdf_to_html.html"
# Load PDF document
document = ap.Document(input_pdf)
# Save PDF in HTML format
save_options = ap.HtmlSaveOptions()
document.save(output_pdf, save_options)
import aspose.pdf as ap
# Load the PDF document
pdf_document = ap.Document("document.pdf")
# Convert to DOCX (Word)
save_options = ap.DocSaveOptions()
save_options.format = ap.DocSaveOptions.DocFormat.DOC_X
# Save the modified document
pdf_document.save("output.docx", save_options)
# Load the PDF document
document = ap.Document("document.pdf")
# Instantiate Document Privileges object
# Apply restrictions on all privileges
documentPrivilege = ap.facades.DocumentPrivilege.forbid_all
# Only allow screen reading
documentPrivilege.allow_screen_readers = True
# Encrypt the file with User and Owner password
# Need to set the password, so that once the user views the file with user password
# Only screen reading option is enabled
document.encrypt("user", "owner", documentPrivilege, ap.CryptoAlgorithm.RC4X128, False)
# Save the encrypted document
document.save("secured_document.pdf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment