2025年9月21日 星期日

只要點兩下兩次,就能依照設定拆分指定PDF頁數並合併成設定好的一個PDF

        最近遇到一個問題,想要對四個不同的PDF,分別擷取1~2頁、3~4頁、5~6頁、7~8頁,合併成一個總共8頁的PDF。那該怎麼寫程式來處理這件事呢?那如果是用線上拆分PDF網頁與線上合併PDF網頁,該怎麼做呢?則是需要將四個不同PDF分別上傳到拆分網頁後再一頁一頁的PDF下載下來,再將所需要頁數的PDF放在同一個資料夾,然後再上傳到合併網頁,最後下到自己的資料夾。不斷地上傳、下載。光是處理這些步驟,所花的時間根本就無法想像。能不能有個程式,會掃描input資料夾內檔案。匯出input資料夾內所有檔案名稱,我們只要設定起始頁面與最後頁面。再對程式點兩下,就會合併所有的頁面到一個PDF。換句話說,只要點兩下兩次,就能依照設定拆分指定PDF頁數並合併成設定好的一個PDF。

下載檔案解壓密碼:demo1234
Here is the website where you can download the program and find instructions:
Download。Extraction Password: demo1234
使用說明(Instructions for use):
    1.下載後,解壓縮。解壓縮的方式要保持原有的資料夾架構,如下:
    意即解壓縮後,會看到內有一個pyPDFDivide.exe與兩個資料夾分別是      
     input 與 source 。如下圖:
    2.將所需的PDF 放入資料夾input 內
    3.滑鼠對pyPDFDivide.exe點兩下,就會產出一個OutputFileName.txt
    4.在OutputFileName.txt,填寫所需的頁數起始範圍後,儲存。
    5.滑鼠對pyPDFDivide.exe點兩下,就會產出output.pdf
    6.該output.pdf即為所求。
使用教學(Instructional videos):



以下是開發過程與原始碼 (Development process and code):
安裝python套件 pikepdf
pip install pikepdf
程式名稱(Program name):pyPDFDivide.py
程式內容(Code):
import os
from pikepdf import Pdf

# 取得目前的工作目錄
current_directory = os.getcwd()
# 取得目前的工作目錄內input資料夾的路徑
current_directory_input = current_directory + "\\input\\"
# 取得工作目錄input資料夾內所有檔案名稱
current_directory_input_files = os.listdir(current_directory_input)

# 檢查 OutputFileName.txt 是否存在
if os.path.exists(current_directory + "\\OutputFileName.txt"):
    # 如果存在,則讀取檔案內容
    with open("OutputFileName.txt", "r", encoding="utf-8") as file:
        content = file.readlines()
else:
    # 如果不存在,則創建 OutputFileName.txt 並寫入內容
    with open("OutputFileName.txt", "w", encoding="utf-8") as file:
        for current_directory_input_file in current_directory_input_files:
            # 開啟每個 PDF 檔案
            pdf = Pdf.open(current_directory_input + current_directory_input_file)
            pages = pdf.pages  # 獲取 PDF 檔案的頁面數量
            # 寫入檔案名稱和頁面數量到 OutputFileName.txt
            file.write("FileName:" + str(current_directory_input_file) + "\nTotalPages:" + str(len(pages)) + "\n" + "StartPage:\nEndPage:\n")

num = 0  # 計數器
FileNameList = []  # 存放檔案名稱的列表
StartPageList = []  # 存放起始頁面的列表
EndPageList = []  # 存放結束頁面的列表

# 解析 OutputFileName.txt 的內容
for i in content:
    if num % 4 == 0:
        # 每四行的第一行是檔案名稱
        temp, tempfilename = i.split(':')
        FileNameList.append(tempfilename.replace("\n", ""))
    elif num % 4 == 2:
        # 每四行的第三行是起始頁面
        temp, tempstartpage = i.split(':')
        StartPageList.append(tempstartpage.replace("\n", ""))
    elif num % 4 == 3:
        # 每四行的第四行是結束頁面
        temp, tempendpage = i.split(':')
        EndPageList.append(tempendpage.replace("\n", ""))
    num = num + 1  # 增加計數器

# 將設定檔 OutputFileName.txt 的內容依序將指定 PDF 拆分合併到 output.pdf
output = Pdf.new()  # 創建一個新的 PDF 檔案
for i in range(len(FileNameList)):
    # 開啟每個指定的 PDF 檔案
    pdf = Pdf.open(current_directory_input + FileNameList[i])
    pages = pdf.pages  # 獲取 PDF 檔案的頁面
    a = int(StartPageList[i])  # 轉換起始頁面為整數
    b = int(EndPageList[i])  # 轉換結束頁面為整數
    # 從起始頁面到結束頁面將頁面添加到新的 PDF 檔案中
    for j in range(a, b + 1):
        output.pages.append(pages[j])
# 儲存合併後的 PDF 檔案
output.save("output.pdf")


2025年9月13日 星期六

只要點兩下就能利用彰化縣EIP系統學生帳號CSV產出指定學生名單的Google Classroom帳號總表

系列文章:
1.只要點兩下就能產出彰化縣Cloud School 學生帳號密碼匯入彰化縣EIP系統學生帳號密碼的CSV
1.https://skjhcreator.blogspot.com/2025/08/cloud-school-eipcsv.html
2.只要點兩下就能利用彰化縣EIP系統學生帳號CSV產出建立Google Classroom 學生總表
2.https://skjhcreator.blogspot.com/2025/08/eipcsvgoogle-classroom.html 
3.
只要點兩下就能利用彰化縣EIP系統學生帳號CSV產出指定學生名單的Google Classroom學生帳號總表
3.https://skjhcreator.blogspot.com/2025/09/eipcsvgoogle-classroom.html

        彰化縣數位學習師生單一帳號系統(EIP=Enterprise Information Portal)於2025年7月7日正式啟用。學務處想利用Google Classroom 來建立全校視訊廣播。因此,給資訊組全年級各班班長的年班座號總表。而資訊組要能產出一個全年級各班班長的Classroom 帳號總表,方便學務處將各班班長加入Google Classroom。 而資訊組只要將這件事簡化,只要點兩下就能得到指定學生名單的Google Classroom帳號總表。然後教學務處如何使用這個程式即可。
        由於EIP系統學生帳號只有帳號,沒有後面的@chc.edu.tw。還要想辦法將@chc.edu.tw加入。但問題是用Excel開啟CSV,學生帳號只有數字,在Excel 格式就會跑掉。比方說學生帳號0432579,在Excel,就會變成432579。若用記事本開,學生帳號"0432579",完全沒法用尋找、取代等方式來簡單完成。就要複製@chc.edu.tw,然後一個個貼上。

1.EIP學生帳號下載方式:
下載的學生資料檔名為學校代碼_學生資料_日期時間.CSV,其格式如下:


下載檔案解壓密碼:demo1234
Here is the website where you can download the program and find instructions:
Download。Extraction Password: demo1234
使用教學(Instructional videos):



以下是開發過程與原始碼 (Development process and code):
程式名稱(Program name):pyStudent2ClassRoomStudentAccount.py
程式內容(Code):
import csv
import os

# 取得目前的工作目錄
current_directory = os.getcwd()
# 取得目前的工作目錄內input
current_directory_input = current_directory+"\\input\\"
# 取得工作目錄input內所有檔案名稱
current_directory_input_files = os.listdir(current_directory_input)
# 以副檔名來判斷設定相對應的檔案
for i in current_directory_input_files:
    TempFileName,TempFileExtension = os.path.splitext(i)
    if TempFileExtension == ".csv":
        TempCSVFile = i
    elif TempFileExtension == ".xlsx":
        TempXLSXFile = i
    else:
        TempTXT = i
             
# 設定CSVList為一個 list
CSVList = []
# 開啟 CSV 檔案
with open(current_directory_input+TempCSVFile, mode='r', encoding='utf-8') as file:
    # 建立 CSV 讀取器
    reader = csv.reader(file)
    # 逐行讀取 CSV 檔案內容
    for row in reader:
        CSVList.append(row)

# CSVListTitle 取得 CSVList[0]
CSVListTitle = CSVList[0]
# 刪除CSVList[0]
CSVList.pop(0)
# 對CSVList 進行排序
CSVList.sort(key=lambda x: x[0])

ResultList = []
ResultList.append(CSVListTitle)

StudentList = []
with open(current_directory_input+TempTXT, "r") as Stufile:
    tempStudentList = Stufile.readlines()

for i in tempStudentList:
    temp = i.strip("\n")
    StudentList.append(temp)

TempStudentSet = set(StudentList)    
   
TempResultSet = set()
for i in StudentList:
    for j in CSVList:
        if str(i) == str(j[0]):
            #print(str(i),str(j[0]))
            ResultList.append([str(j[0]),str(j[1])+"@chc.edu.tw"])
            TempResultSet.add(str(i))
           
ExceptionSet = TempStudentSet - TempResultSet
ExceptionList = list(ExceptionSet)
           
with open(current_directory+"\\Student2ClassRoomStudentAccount.csv","w", newline="", encoding="utf-8") as file:
    writer = csv.writer(file)
    writer.writerows(ResultList)
print("Student2ClassRoomStudentAccount.csv 檔案已成功寫入!")    

with open(current_directory+"\\Student2ClassRoomStudentAccountException.txt", "w",encoding="utf-8") as file:
    file.write("Excel 找不到的學生帳號如下:\n")
    for i in ExceptionList:
        file.write(str(i)+"\n")
print("Student2ClassRoomStudentAccountException.csv 檔案已成功寫入!")



2025年8月28日 星期四

只要點兩下就能利用彰化縣EIP系統學生帳號CSV產出建立Google Classroom 學生總表

系列文章:
1.只要點兩下就能產出彰化縣Cloud School 學生帳號密碼匯入彰化縣EIP系統學生帳號密碼的CSV
1.https://skjhcreator.blogspot.com/2025/08/cloud-school-eipcsv.html
2.只要點兩下就能利用彰化縣EIP系統學生帳號CSV產出建立Google Classroom 學生總表
2.https://skjhcreator.blogspot.com/2025/08/eipcsvgoogle-classroom.html 
3.
只要點兩下就能利用彰化縣EIP系統學生帳號CSV產出指定學生名單的Google Classroom學生帳號總表
3.https://skjhcreator.blogspot.com/2025/09/eipcsvgoogle-classroom.html

        彰化縣數位學習師生單一帳號系統(EIP=Enterprise Information Portal)於2025年7月7日正式啟用。我要如何利用EIP系統學生帳號CSV來建立Google Classroom 後,將各班的學生加入。或是給老師一張學生帳號總表,讓老師自行建立Classroom,然後將學生加入。由於EIP系統學生帳號只有帳號,沒有後面的@chc.edu.tw。還要想辦法將@chc.edu.tw加入。但問題是用Excel開啟CSV,學生帳號只有數字,在Excel 格式就會跑掉。比方說學生帳號0432579,在Excel,就會變成432579。若用記事本開,學生帳號"0432579",完全沒法用尋找、取代等方式來簡單完成。就要複製@chc.edu.tw,然後一個個貼上。

1.EIP學生帳號下載方式:
下載的學生資料檔名為學校代碼_學生資料_日期時間.CSV,其格式如下:


下載檔案解壓密碼:demo1234
Here is the website where you can download the program and find instructions:
Download。Extraction Password: demo1234
使用教學(Instructional videos):



以下是開發過程與原始碼 (Development process and code):
程式名稱(Program name):pyEIP2GoogleClassRoomStudentAccount.py
程式內容(Code):
import csv
import os

# 取得目前的工作目錄
current_directory = os.getcwd()
# 取得目前的工作目錄內input
current_directory_input = current_directory+"\\input\\"
#  取得工作目錄input內所有檔案名稱
current_directory_input_files = os.listdir(current_directory_input)

# 以副檔名來判斷設定相對應的檔案
for i in current_directory_input_files:
    TempFileName,TempFileExtension = os.path.splitext(i)
    if TempFileExtension == ".csv":
        TempCSVFile = i
    elif TempFileExtension == ".xlsx":
        TempXLSXFile = i
    else:
        TempTXT = i

# 設定CSVList為一個 list
CSVList = []
# 開啟 CSV 檔案
with open(current_directory_input+TempCSVFile, mode='r', encoding='utf-8') as file:
    # 建立 CSV 讀取器
    reader = csv.reader(file)
    # 逐行讀取 CSV 檔案內容
    for row in reader:
        CSVList.append(row)

# CSVListTitle 取得 CSVList[0]
CSVListTitle = CSVList[0]
# 刪除CSVList[0]
CSVList.pop(0)
# 對CSVList 進行排序
CSVList.sort(key=lambda x: x[0])
ResultList = []
ResultList.append(CSVListTitle)

for j in CSVList:
    ResultList.append([str(j[0]),str(j[1])+"@chc.edu.tw"])
               
with open(current_directory+"\\EIP2GoogleClassRoomStudentAccount.csv", mode="w", newline="", encoding="utf-8") as file:
    writer = csv.writer(file)
    writer.writerows(ResultList)

print("EIP2GoogleClassRoomStudentAccount.csv 檔案已成功寫入!")

資料來源:

只要點兩下就能產出彰化縣Cloud School 學生帳號密碼匯入彰化縣EIP系統學生帳號密碼的CSV

系列文章:
1.只要點兩下就能產出彰化縣Cloud School 學生帳號密碼匯入彰化縣EIP系統學生帳號密碼的CSV
1.https://skjhcreator.blogspot.com/2025/08/cloud-school-eipcsv.html
2.只要點兩下就能利用彰化縣EIP系統學生帳號CSV產出建立Google Classroom 學生總表
2.https://skjhcreator.blogspot.com/2025/08/eipcsvgoogle-classroom.html 
3.
只要點兩下就能利用彰化縣EIP系統學生帳號CSV產出指定學生名單的Google Classroom學生帳號總表
3.https://skjhcreator.blogspot.com/2025/09/eipcsvgoogle-classroom.html

        彰化縣數位學習師生單一帳號系統(EIP=Enterprise Information Portal)於2025年7月7日正式啟用。由於EIP、Cloud School 會進行資料同步,但密碼卻不會一致。身為一個教師,讓學生記多組帳號密碼,那是一件愚蠢的事。換句話說,資訊組長需要手動進行,讓EIP、Cloud School密碼一致。但問題是Cloud School匯出的表為all.xlsx,彰化縣EIP系統學生帳號密碼匯入的格式是CSV。更嚴重的是兩者的欄位格式完全不同、排序也不同。如果一次下載全校學生的資料,需要手動修改的地方就會很多。雖然一年只要做一次,但是想到每年都要做,就讓人覺得心煩。有沒有辦法,讓整件事變得簡單。例如,下載兩個系統的資料後,只要滑鼠點兩下,就產出彰化縣EIP系統學生帳號密碼匯入的CSV。然後一次匯入,就結束。
        以下為問題描述,都是直接用Excel開啟:
1.
Cloud School 學生資料下載方式:

下載的學生資料檔名為all.xlsx,其格式如下:
學生名單從70101開始往下排。
2.
EIP學生帳號下載方式:
下載的學生資料檔名為學校代碼_學生資料_日期時間.CSV,其格式如下:
學生名單從90101開始往下排。學生帳號如上圖的第7個,只知道是4開頭的數字,但不知道是04開頭的,還是4開頭的。若要確認,還要用記事本去開啟CSV,才能夠確認。若是直接用Excel開啟後,直接複製密碼,貼上另一個檔案。儲存之後,學生帳號如上圖的第7個,格式就會被存成4開頭的數字。還要人工一個個檢查。

下載檔案解壓密碼:demo1234
Here is the website where you can download the program and find instructions:
Download。Extraction Password: demo1234
使用教學(Instructional videos):

以下是開發過程與原始碼 (Development process and code):
安裝套件openpyxl
指令(command):
pip install openpyxl
 
程式名稱(Program name):pyCloudSchool_pwd2EIP_pwd.py
程式內容(Code):
import csv
import os
from openpyxl import load_workbook

# 取得目前的工作目錄
current_directory = os.getcwd()
# 取得目前的工作目錄內input
current_directory_input = current_directory+"\\input\\"
#  取得工作目錄input內所有檔案名稱
current_directory_input_files = os.listdir(current_directory_input)

# 以副檔名來判斷設定相對應的檔案
for i in current_directory_input_files:
    TempFileName,TempFileExtension = os.path.splitext(i)
    if TempFileExtension == ".csv":
        TempCSVFile = i
    elif TempFileExtension == ".xlsx":
        TempXLSXFile = i
    else:
        TempTXT = i

# 設定CSVList為一個 list
CSVList = []
# 開啟 CSV 檔案
with open(current_directory_input+TempCSVFile, mode='r', encoding='utf-8') as file:
    # 建立 CSV 讀取器
    reader = csv.reader(file)
    # 逐行讀取 CSV 檔案內容
    for row in reader:
        CSVList.append(row)

# 設定 ExcelList 為一個list
ExcelList = []
# 載入 Excel 文件
workbook = load_workbook(current_directory_input+TempXLSXFile)
# 取得所有工作表名稱
sheet_names = workbook.sheetnames
# 選擇特定工作表
sheet = workbook[sheet_names[0]]
# 讀取工作表內容
for row in sheet.iter_rows(values_only=True):
    ExcelList.append([str(row[0])+str(row[1]).zfill(2)+str(row[2]).zfill(2),row[7]])
# 關閉工作簿
workbook.close()
# CSVListTitle 取得 CSVList[0]
CSVListTitle = CSVList[0]
# 刪除CSVList[0]
CSVList.pop(0)
# 對CSVList 進行排序
CSVList.sort(key=lambda x: x[0])
# ExcelListTitle 取得 CSVList[0]
ExcelListTitle = ExcelList[0]
# 刪除ExcelList[0]
ExcelList.pop(0)
# 對ExcelList 進行排序
ExcelList.sort(key=lambda x: x[0])
# 複製一份ExcelList
CopyExcelList = ExcelList[:]

ResultList = []
ResultList.append(CSVListTitle)

# 當兩者檔案學生名單長度不一致
if len(ExcelList)-len(CSVList) != 0:
    for i in ExcelList:
        for j in CSVList:
            if i[0] == j[0]:
                ResultList.append([str(j[0]),str(j[1]),str(i[1])])
                CopyExcelList.remove(i)
               
    # 印出不一致的學生名單
    #print("印出不一致的學生名單:")                
    #print(CopyExcelList)
    with open(current_directory+"\\CloudSchool_pwd2EIP_pwd_Exception.txt", "w",encoding="utf-8") as file:
        file.write("印出不一致的學生名單:\n")
        for i in CopyExcelList:
            file.write(str(i)+"\n")
    print("CloudSchool_pwd2EIP_pwd_Exception.txt 檔案已成功寫入!")
else: # 當兩者檔案學生名單長度一致
    for i in range(len(CSVList)):
        if ExcelList[i][0] == CSVList[i][0]:
            ResultList.append([str(CSVList[i][0]),str(CSVList[i][1]),str(ExcelList[i][1])])

with open(current_directory+"\\CloudSchool_pwd2EIP_pwd.csv", mode="w", newline="", encoding="utf-8") as file:
    writer = csv.writer(file)
    writer.writerows(ResultList)

print("CloudSchool_pwd2EIP_pwd.csv檔案已成功寫入!")




資料來源:



只要點兩下兩次,就能依照設定拆分指定PDF頁數並合併成設定好的一個PDF

        最近遇到一個問題,想要對四個不同的PDF,分別擷取1~2頁、3~4頁、5~6頁、7~8頁,合併成一個總共8頁的PDF。那該怎麼寫程式來處理這件事呢?那如果是用線上拆分PDF網頁與線上合併PDF網頁,該怎麼做呢?則是需要將四個不同PDF分別上傳到拆分網頁後再 一頁一...