Skip to content

Instantly share code, notes, and snippets.

View fileformat-blog-gists's full-sized avatar

fileformat-blog-gists

View GitHub Profile
@fileformat-blog-gists
fileformat-blog-gists / edit-xml-file.py
Created March 26, 2025 12:11
Editing XML Files with Python
import xml.etree.ElementTree as ET
tree = ET.parse('books.xml')
root = tree.getroot()
for book in root.findall('book'):
if book.get('id') == '1':
book.find('price').text = '24.99'
tree.write('books.xml')
@fileformat-blog-gists
fileformat-blog-gists / read-xml-file.js
Created March 26, 2025 11:54
Reading XML Files Using JavaScript (Browser)
let xmlString = `<?xml version="1.0"?><books><book><title>XML Guide</title></book></books>`;
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlString, "text/xml");
console.log(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue);
@fileformat-blog-gists
fileformat-blog-gists / read-xml-file.java
Created March 26, 2025 11:51
Reading XML Files Using Java
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class ReadXML {
public static void main(String[] args) throws Exception {
File inputFile = new File("books.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputFile);
@fileformat-blog-gists
fileformat-blog-gists / read-xml-file.py
Created March 26, 2025 11:48
Reading XML Files Using Python
import xml.etree.ElementTree as ET
tree = ET.parse('books.xml')
root = tree.getroot()
for book in root.findall('book'):
title = book.find('title').text
author = book.find('author').text
price = book.find('price').text
print(f"Title: {title}, Author: {author}, Price: {price}")
@fileformat-blog-gists
fileformat-blog-gists / simple-xml-file.xml
Created March 26, 2025 11:45
An XML file consists of elements, attributes, and hierarchical data. Here’s a simple XML example:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="1">
<title>Introduction to XML</title>
<author>John Doe</author>
<price>29.99</price>
</book>
<book id="2">
<title>Advanced XML Techniques</title>
<author>Jane Smith</author>
@fileformat-blog-gists
fileformat-blog-gists / watermark-pdf-using-pypdf-in-python.py
Created January 30, 2025 15:27
This Python script demonstrates how to add a watermark to a PDF file using the PyPDF library.
from pypdf import PdfReader, PdfWriter
# Paths for the input PDF and watermark PDF
input_pdf = "pdf-to-watermark/input.pdf"
watermark_pdf = "pdf-to-watermark/watermark.pdf"
output_pdf = "pdf-to-watermark/output_with_watermark.pdf"
# Read the input PDF and watermark PDF
reader = PdfReader(input_pdf)
watermark = PdfReader(watermark_pdf)
@fileformat-blog-gists
fileformat-blog-gists / split-pdf-using-pypdf-in-python.py
Created January 29, 2025 15:27
This Python script demonstrates how to split a PDF into separate PDFs using the PyPDF library.
from pypdf import PdfReader, PdfWriter
import os
# Input PDF file
input_pdf = "pdf-to-split/input.pdf"
# Output directory
output_directory = "pdf-to-split/output-dir"
# Ensure the output directory exists
@fileformat-blog-gists
fileformat-blog-gists / merge_pdf_files_using_pypdf_in_python.py
Created January 29, 2025 06:33
This Python script demonstrates how to merge multiple PDF files into a single PDF using the PyPDF library.
import os
from pypdf import PdfReader, PdfWriter
# Directory containing PDF files
directory = "pdfs-to-merge"
# Output file name
output_file = "output-dir/merged_output.pdf"
# Ensure the output directory exists
@fileformat-blog-gists
fileformat-blog-gists / rotate-pdf-page-using-pypdf-in-python.py
Created January 29, 2025 05:08
This Python script demonstrates how to rotate a PDF page using the PyPDF library in Python.
from pypdf import PdfReader, PdfWriter
# Input and output PDF files
input_pdf = "pdf-to-rotate/input.pdf"
output_pdf = "pdf-to-rotate/rotated_output.pdf"
# Read the PDF
reader = PdfReader(input_pdf)
writer = PdfWriter()
@fileformat-blog-gists
fileformat-blog-gists / Output - extract-text-from-pdf-using-pypdf-in-python.txt
Created January 29, 2025 03:02
Output - extract-text-from-pdf-using-pypdf-in-python.py
Page 1:
Python Tutorial
Release 3.7.0
Guido van Rossum
and the Python development team
September 02, 2018
Python Software Foundation
Email: docs@python.org
--------------------------------------------------
Page 2: