2022年9月30日 星期五

Rename Multiple Files Using Python

Using python to rename multiple files


 import os


rootdir = r'target'

str = "08"

for filename in os.listdir(rootdir):

    if str in filename:

        filepath = os.path.join(rootdir, filename)

        newfilepath = os.path.join(rootdir, filename.replace(str, "09"))

        os.rename(filepath, newfilepath)

Rename a file using Python pathlib module

Rename a file using Python pathlib module

 from pathlib import Path


path = Path("target file")


target = path.with_name(path.name.replace("08",'09'))


path.rename(target)

Convert PDF to xlsx file Using Python pandas and openxl module

#  Convert PDF file to CSV file

 #  Reference : https://www.youtube.com/watch?v=wJwXThPSBE8  ( From 3:48 )

import tabula


tabula.io.convert_into('A_PDF_file.pdf','A_CSV_file.csv', output_format='csv', pages='all')


#  Convert CSV file to xlsx file

#  https://www.youtube.com/watch?v=JFAn-aownHE  ( From 3:45 )



import csv

import openpyxl


csv_data = []

with open('A_CSV_file.csv') as file_obj:

    reader = csv.reader(file_obj)

    for row in reader:

        csv_data.append(row)


wb = openpyxl.Workbook()

sheet = wb.active

for row in csv_data:

    sheet.append(row)


wb.save('A_.xlsx')

Convert PDF to CSV file Using Python tabula module

 Use python tabula module to convert PDF to csv file

import tabula


tabula.io.convert_into('A_PDF_file.pdf', 'A_CSV_file.csv', output_format='csv', pages='all')

Combine multiple excel files Using python pandas module

Combine multiple  Excel files using python pandas module


from operator import index

import pandas as pd

import datetime as dt


files = ['1_data.xlsx','2_data.xlsx','3_data.xlsx','4_data.xlsx','5_data.xlsx','6_data.xlsx','7_data.xlsx','8_data.xlsx']

combined = pd.DataFrame()


for file in files:

    df=pd.read_excel(file)

    combined=combined.append(df,ignore_index=True)


combined.to_excel('combine.xlsx',index=False,sheet_name='data')

Python program to display calendar

# Python program to display calendar of given month of the year # importing calendar module for calendar operations import calendar # set t...