LoginSignup
1

More than 1 year has passed since last update.

【Python】複数エクセルファイルのPDF生成を2分で終わらせてみた

Posted at

◇作ろうと思ったきっかけ

複数のエクセルファイルをPDF保存することがめんどうになり、作成しました。

◇このツールで出来ること

1.複数のエクセルファイルをそれぞれPDF保存
2.保存したPDFを特定のフォルダ配下に配置

◇言語バージョン

C:\Users\test>python --version
Python 3.8.10

◇実装

# ---------PDFを作成---------
def save_pdf(file_path, new_file_path):
    file_pdf = new_file_path[:-4]+"pdf"
    app = com.Dispatch("Excel.Application")
    app.Visible = False
    app.DisplayAlerts = False
    book = app.Workbooks.Open(file_path)
    # 特定のシートを指定
    book.Worksheets("Sheet1").Activate()
    xlTypePDF = 0
    book.ActiveSheet.ExportAsFixedFormat(xlTypePDF, file_pdf)
    app.Quit()
    # エクセルが完全に落ちるまで待機
    time.sleep(1)

# ---------配置用のフォルダを作成---------
def make_folder(file_path):
    file_dir_path = os.path.dirname(file_path)
    file_name = os.path.basename(file_path)
    dir_name = "【新規フォルダ】" + file_name
    file_dir_path = "\\Users\\test\\デスクトップ\\配置後/" + dir_name
    os.mkdir(file_dir_path)
    new_file_path = file_dir_path + '/' + file_name
    save_pdf(file_path,new_file_path) 

# ---------テキストエリアのパスを成型---------
def arrange_file_path():
    file_path = text_box.get("1.0","end")
    file_path = file_path.replace('"','')
    file_path = file_path.replace('\\', '/')
    file_path = file_path[:-1] 
    arrange_file_path = []
    arrange_file_path = file_path.split('\n')
    return arrange_file_path

# ---------テキストエリアのパスを取得---------
def get_text():
    files = arrange_file_path()
    for file_path in files:
        make_folder(file_path)

# set Frame
root = tk.Tk()
root.geometry("500x200")

# make component
text_box = tk.Text()
text_box["width"] = 80
text_box["height"] = 13
button = tk.Button()
button["text"] = "Button"
button["command"] = get_text

#set component
text_box.pack()
button.pack()

# set focus
text_box.focus_set()

root.mainloop()

◇詳細

コピー元のファイルを張り付けるため、テキストエリアの枠とボタンを作成しています。

# set Frame
root = tk.Tk()
root.geometry("500x200")

# make component
text_box = tk.Text()
text_box["width"] = 80
text_box["height"] = 13
button = tk.Button()
button["text"] = "Button"
button["command"] = get_text

#set component
text_box.pack()
button.pack()

# set focus
text_box.focus_set()

root.mainloop()

画面イメージは以下の通り
画像1.PNG

def get_text():

テキストエリアに張りつけたファイルパスを取得しています。

def get_text():
    files = arrange_file_path()
    for file_path in files:
        make_folder(file_path)

def arrange_file_path():

ファイルの配置とフォルダ作成用に、取得したファイルパスを成型しています。
また、複数ファイルを取得したとき用にリストへファイルパスを格納しています。

def arrange_file_path():
    file_path = text_box.get("1.0","end")
    file_path = file_path.replace('"','')
    file_path = file_path.replace('\\', '/')
    file_path = file_path[:-1] 
    arrange_file_path = []
    arrange_file_path = file_path.split('\n')
    return arrange_file_path

def make_folder(file_path):

テキストエリアに張り付けられたファイルパスをもとに、PDFファイル配置用のフォルダを作成しています。

def make_folder(file_path):
    file_dir_path = os.path.dirname(file_path)
    file_name = os.path.basename(file_path)
    dir_name = "【新規フォルダ】" + file_name
    file_dir_path = "\\Users\\test\\デスクトップ\\配置後/" + dir_name
    os.mkdir(file_dir_path)
    new_file_path = file_dir_path + '/' + file_name
    save_pdf(file_path,new_file_path) 

画面イメージは以下の通り
参考画像3.PNG

def save_pdf(file_path, new_file_path):

フォルダ名称と配置先を指定し、PDFファイルを保存しています。

def save_pdf(file_path, new_file_path):
    file_pdf = new_file_path[:-4]+"pdf"
    app = com.Dispatch("Excel.Application")
    app.Visible = False
    app.DisplayAlerts = False
    book = app.Workbooks.Open(file_path)
    # 特定のシートを指定
    book.Worksheets("Sheet1").Activate()
    xlTypePDF = 0
    book.ActiveSheet.ExportAsFixedFormat(xlTypePDF, file_pdf)
    app.Quit()
    # エクセルが完全に落ちるまで待機
    time.sleep(1)

画面イメージは以下の通り
参考画像4.PNG

◇使い方

1.当該ツールを実行し、テキストエリアを表示
2.PDF保存したいエクセルファイルの絶対パスをコピーし、テキストエリアに張り付ける
 ※ダブルクオーテーションはつけたままで問題ない
3.ファイルパスに誤りがないことを確認し、OKを押す
4.フォルダを作成し、PDFファイルの保存が完了

◇感想

都度複数のPDFファイルを作成する機会があり、重宝しています。

最後までご覧いただきありがとうございました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1