Convert Text to PDF in Python

The ‘reportlab‘ library can be used to convert text to PDF in Python. If you haven’t already done so, install the reportlab library using this command: Copy pip install reportlab Examples of Converting Text to PDF in Python Example#1: Single SHORT Sentence to PDF The following script can be used to convert a single short … Read more

Read JSON Files using Python

The ‘json‘ module can be used to read JSON files in Python. In this short guide, you’ll see several examples of reading JSON files using Python. Example of Reading a JSON File in Python Here is an example of a simple JSON file called example.json: To read that JSON file using Python: Copy import json … Read more

Access and Modify Excel Files using Python

The OpenPyXL package can be used to access and modify Excel files in Python. In this short tutorial, you’ll see how to use this package to access and modify cells in Excel. Steps to Access and Modify Excel Files using Python (1) Installation To install the OpenPyXL package, use the following command: Copy pip install … Read more

Schedule a Task using Python

The ‘schedule‘ library can be used to schedule a task in Python. The command to install the ‘schedule‘ library is: Copy pip install schedule (1) To schedule a task in Python to run every day (for example, at 10:00 am every day): Copy import schedule import time def daily_task(): print(“This task runs every day”) # … Read more

How to Delete a File using Python

The ‘os‘ module can be used to delete a file in Python: Copy import os file_path = r”Path where the file is stored\file_name.file_extension” os.remove(file_path) Example of Deleting a File using Python (1) Let’s suppose that a file called ‘Products‘ is stored in a ‘Test‘ folder, where the full file path is: C:\Users\Ron\Desktop\Test\Products.csv (2) To delete … Read more

Check if a Substring Exists in a String in Python

Here are 2 ways to check if a substring exists in a string in Python: (1) Using the “in” keyword: Copy my_string = “This is an example of a string” my_substring = “example” if my_substring in my_string: print(“Substring found”) else: print(“Substring not found”) The result: (2) Using the “find()” method: Copy my_string = “This is … Read more

How to Append Data to an Existing File using Python

To append data to an existing file using Python: Copy # The new data to be appended new_data = ” This is the additional data to be added to the file” # File path, including the r letter before the path to avoid unicode errors file_path = r”path to file\file_name.file_extension” # Opening the file in … Read more

Use Python to get the File Paths within a Folder

Here are 3 ways to get the file paths that match specific patterns within a folder: (1) Fetch all CSV files within the specified folder Copy import glob folder_path = r”C:\Users\Ron\Desktop\Test” csv_files = glob.glob(folder_path + r”\*.csv”) print(csv_files) Where: The result is a list containing the full paths of 3 CSV files inside the “Test” folder: … Read more

How to Write Data to a JSON File using Python

Here are 3 ways to write data to a json file using Python: (1) Single dictionary to a json file: Copy import json data = { “product”: “Computer”, “price”: 1300, “brand”: “A” } file_path = r”path to save the json file\file_name.json” with open(file_path, “w”) as file: json.dump(data, file, indent=4) Where: The resulted json file: Copy … Read more

Write List of Lists to CSV File in Python

To write a List of Lists to a CSV file in Python: Copy import csv data = [ [“value_1”, “value_2”, “value_3”], [“value_4”, “value_5”, “value_6”], [“value_7”, “value_8”, “value_9″] ] file_path = r”path to store the file\file_name.csv” with open(file_path, “w”, newline=””) as file: csv_writer = csv.writer(file) csv_writer.writerows(data) Example of Writing a List of Lists to a CSV … Read more