1. 程式人生 > 其它 >excel批量寫入到word模板中

excel批量寫入到word模板中

技術標籤:python

可以插入圖片或文字,初學,搞不懂類和tk,程式我打包到下載了/(ㄒoㄒ)/~~

import xlrd
from docx import Document
from docx.shared import Cm
import tkinter as tk
from tkinter import filedialog, messagebox
from os import system

'''開啟選擇資料夾對話方塊'''
window = tk.Tk()
window.title("Excel批量生成word 1.0 ")
window.geometry(
'380x180+800+200') # 290 160為視窗大小,+1000 +10 定義視窗彈出時的預設展示位置 window.resizable(0, 0) window['background'] = 'DimGray' def help_text(): system("start ./help\help.docx") def about(): tk.messagebox.showinfo('關於', "Excel批量生成word1.0\n \n\nby:放放風 2021.01.23 \n更多功能請聯絡:[email protected]") menubar =
tk.Menu(window) file_menu = tk.Menu(menubar, tearoff=False) file_menu.add_command(label='幫助文件', command=help_text) menubar.add_cascade(label='選單', menu=file_menu) menubar.add_command(label='關於', command=about) # menubar.add_command(label='選單')#command="print_hello" window.config(menu=
menubar) v = tk.IntVar() class P: def __init__(self): self.docx_path = "" self.xls_path = "" self.Save_path = "" self.pic_path = [] def open_word(self): # 開啟 docx_path = filedialog.askopenfilenames(filetypes=[("文字", "docx")], title="選擇模板") # 獲取需要轉換的規劃檔案絕對路徑 if not docx_path: tk.messagebox.showinfo('警告', "請選擇docx!") else: self.docx_path = docx_path def open_excel(self): xls_path = filedialog.askopenfilenames(filetypes=[("文字", "xls")], title="選擇資料") # 獲取需要轉換的規劃檔案絕對路徑 if not xls_path: tk.messagebox.showinfo('警告', "請選擇xls!") else: self.xls_path = xls_path def open_pic(self): for count in range(int(text0_var.get())): pic_path = filedialog.askopenfilenames(filetypes=[("jpg", "jpg"), ("png", "png")], title="選擇資料") # 獲取需要轉換的規劃檔案絕對路徑 if not pic_path: tk.messagebox.showinfo('警告', "請選擇圖片!") else: self.pic_path.append(pic_path) def save(self): # 儲存 Save_path = filedialog.askdirectory() if not Save_path: tk.messagebox.showinfo('警告', "請選擇儲存路徑") else: self.Save_path = Save_path myobject = P() def conversion(): global D myobject.save() if not myobject.Save_path: return '''選擇word模板''' Word_path = myobject.docx_path '''選擇excel資料''' Excel_path = myobject.xls_path count = int(text0_var.get()) tb = [int(t) for t in text1_var.get().split(",")] cell_r = [int(t) for t in text2_var.get().split(",")] cell_c = [int(t) for t in text3_var.get().split(",")] for path in Excel_path: # wb = xlrd.open_workbook(path) ws = wb.sheet_by_index(0) for xr in range(1, ws.nrows): # 迴圈行 document = Document(Word_path[0]) ctitle = ws.cell_value(xr, 0) if count != 0: pp = myobject.pic_path for g in range(count): if len(tb) == 1: A = tb[0] else: A = tb[g] B = cell_c[g] C = cell_r[g] if count == 1: D = pp[0] elif count > 1: D = pp[g] for pic in D: if ctitle in pic: pun = document.tables[A].cell(B, C).paragraphs[0].add_run() insert_p = pun.add_picture(pic) insert_p.height = Cm(4) insert_p.width = Cm(10) for xc in range(1, ws.ncols): # 迴圈列 val = ws.cell_value(xr, xc) val = str(val) title = ws.cell_value(0, xc) for p in document.paragraphs: # 迴圈段落 for run in p.runs: if title in run.text: run.text = run.text.replace(title, val, 1) for t in document.tables: # 迴圈表格 for row in t.rows: for cell in row.cells: for cp in cell.paragraphs: for crun in cp.runs: if title == crun.text: crun.text = crun.text.replace(title, val, 1) name = ws.cell_value(xr, 0) document.save(myobject.Save_path + "\\" + name + ".docx") tk.messagebox.showinfo('提示', "成功,請檢查資料") tk.Button(window, text="選擇Word", width=15, height=1, command=myobject.open_word, bg="Silver").grid(row=2, column=4) tk.Button(window, text="選擇Excel", width=15, height=1, command=myobject.open_excel, bg="Silver").grid(row=3, column=4) tk.Button(window, text="選擇照片", width=15, height=1, command=myobject.open_pic, bg="Silver").grid(row=4, column=4) tk.Button(window, text="開始", width=15, height=1, command=conversion, bg="Silver").grid(row=5, column=4) lable1 = tk.Label(window, text="[表]", width=15).grid(row=0, column=0) text1_var = tk.StringVar() # 獲取text_1輸入的值 text1_var.set(r'0') text1 = tk.Entry(window, textvariable=text1_var, bd=5).grid(row=0, column=1) lable2 = tk.Label(window, text="[行]", width=15).grid(row=1, column=0) text2_var = tk.StringVar() # 獲取text_1輸入的值 text2_var.set(r'0') text2 = tk.Entry(window, textvariable=text2_var, bd=5).grid(row=1, column=1) lable3 = tk.Label(window, text="[列]", width=15).grid(row=2, column=0) text3_var = tk.StringVar() # 獲取text_1輸入的值 text3_var.set(r'0') text3 = tk.Entry(window, textvariable=text3_var, bd=5).grid(row=2, column=1) lable4 = tk.Label(window, text="[照片width]", width=15).grid(row=3, column=0) text4_var = tk.StringVar() # 獲取text_1輸入的值 text4_var.set(r'0') text4 = tk.Entry(window, textvariable=text4_var, bd=5).grid(row=3, column=1) lable5 = tk.Label(window, text="[照片height]", width=15).grid(row=4, column=0) text5_var = tk.StringVar() # 獲取text_1輸入的值 text5_var.set(r'0') text5 = tk.Entry(window, textvariable=text5_var, bd=5).grid(row=4, column=1) lable0 = tk.Label(window, text="[照片個數]", width=15).grid(row=5, column=0) text0_var = tk.StringVar() # 獲取text_1輸入的值 text0_var.set(r'0') text0 = tk.Entry(window, textvariable=text0_var, bd=5).grid(row=5, column=1) cjip = tk.PhotoImage(file=".\\help\\a16.gif") tk.Label(window, image=cjip, height=50, width=110).grid(row=0, column=4, rowspan=2, columnspan=2) window.mainloop()