""" 本程序会把py文件以表格的形式写入到word文档中 """ import os from docx import Document from docx.shared import Inches import tkinter as tk from tkinter import filedialog as fd def callback(): name= fd.askopenfilename(initialdir = ".", title = "选择Python源代码文件", filetypes = (("py files","*.py"),("所有文件","*.*"))) if name != '': f = open(name,encoding='utf-8') c = f.read() f.close() filecontents = c.split('\n') document = Document() table = document.add_table(rows=len(filecontents), cols=2) i = 0 for line in filecontents: print(line) row = table.rows[i].cells row[0].text = str(i+1) row[1].text = line i = i + 1 document.save('dir_py.docx') tk.Button(text='单击选择一个Python源文件', command=callback).pack(fill=tk.X) tk.mainloop()
李兴球
李兴球的博客是Python创意编程原创博客
要发表评论,您必须先登录。
发表评论