docx參考手冊:python文檔 — python-docx 0.8.11 文檔 (osgeo.cn) https://www.osgeo.cn/openpyxl/index.html
OpenPYXL參考手冊 :開發 — openpyxl 3.0.5 文檔 (osgeo.cn) https://www.osgeo.cn/openpyxl/development.htm
需求:將一個Excel工作表的B列前5個單元格內容轉為word的5個段落。
參考:操作word python操作word
操作excel python openpyxl自動化操作excel(xlsx)
代碼:
#-*- coding: utf-8 -*- import os,openpyxl,glob from openpyxl.styles import Border, Side,PatternFill from docx import Document from docx.shared import Pt,Cm,Inches from docx.oxml.ns import qn from docx.enum.style import WD_STYLE_TYPE from docx.enum.text import WD_LINE_SPACING if __name__ == '__main__': #word Doc = Document() filelist=[] i=0 for filename in glob.glob(r'*.xlsx'): filelist.append(filename) print(i,filename) i=i+1 f_num = input("請輸入EXCEL文件序號:") file = filelist[int(f_num)] #讀取EXCEL的B列前5個單元格 wb = openpyxl.load_workbook(file) ws = wb.active i=1 for cell in ws["B"]: if i>5: break i=i+1 #存入word Doc.add_paragraph(str(cell.value)) Doc.save("example.doc")
類似的:
#-*- coding: utf-8 -*- from openpyxl import Workbook from openpyxl import load_workbook import openpyxl from docx import Document from docx.shared import Pt,Cm from docx.oxml.ns import qn #global items = [] file1 = r"F:\統計表.xlsx" #新建word Doc = Document() print('新建成功') #讀取數據 wb1=openpyxl.load_workbook(file1) #獲取sheet頁 sheet1=wb1._sheets[0] for m in range(1,100): v = sheet1["H"+str(m)].value if v == None: pass else: w =v.split("\n") for x in w: items.append(x) wb1.close()#關閉excel for n in range(1,len(items)): Doc.add_paragraph(items[n]) #段落格式 for p in Doc.paragraphs: paragraph_format=p.paragraph_format paragraph_format.space_before=Pt(0) #上行間距 paragraph_format.space_after=Pt(0) #下行間距 paragraph_format.line_spacing=Pt(13) #行距 #字體 for run in p.runs: run.font.size=Pt(12) run.font.name='宋體' # 設置像黑體這樣的中文字體,必須添加下面 2 行代碼 r = run._element r.rPr.rFonts.set(qn("w:eastAsia"),"黑體") #頁邊距 #changing the page margins margin =1.0 sections = Doc.sections for section in sections: section.top_margin = Cm(margin) section.bottom_margin = Cm(margin) section.left_margin = Cm(margin) section.right_margin = Cm(margin) Doc.save('test.doc')#保存數據
改成更通用的
用鼠標拖放xlsx文件 ,在同目錄下生成docx,並限定要轉換的列和行
#-*- coding: utf-8 -*- from openpyxl import Workbook from openpyxl import load_workbook import openpyxl from docx import Document from docx.shared import Pt,RGBColor,Cm from docx.oxml.ns import qn from docx.enum.text import WD_ALIGN_PARAGRAPH #設置對其方式 from tkinter import * import tkinter from tkinter.messagebox import showinfo import windnd import os #要獲取的列 cols =['O','P','I'] #要獲取的最大行號 maxrow =100 #xlsx轉docx def convxlsx(file1): items = [] #新建word Doc = Document() #頁邊距 section = Doc.sections[0] section.top_margin = Cm(2.54) section.bottom_margin = Cm(2.54) section.left_margin = Cm(3.18) section.right_margin = Cm(3.18) #讀取數據 wb1=openpyxl.load_workbook(file1) #獲取sheet頁 sheet1=wb1._sheets[0] for m in range(1,maxrow): for N in cols: v = sheet1[N+str(m)].value if v == None: pass else: p = Doc.add_paragraph(v) p.style.font.size=Pt(16) p.alignment = WD_ALIGN_PARAGRAPH.LEFT p.paragraph_format.first_line_indent = p.style.font.size * 2 if N=="O": p.paragraph_format.space_before=Pt(0) #上行間距 p.paragraph_format.space_after=Pt(0) #下行間距 for run in p.runs: run.font.name='方正楷體簡體' run._element.rPr.rFonts.set(qn('w:eastAsia'), '方正楷體簡體') run.font.color.rgb=RGBColor(0,0,0) run.font.size=Pt(16) else: p.paragraph_format.space_before=Pt(0) #上行間距 p.paragraph_format.space_after=Pt(0) #下行間距 for run in p.runs: run.font.name='方正仿宋簡體' run._element.rPr.rFonts.set(qn('w:eastAsia'), '方正仿宋簡體') run.font.color.rgb=RGBColor(0,0,0) run.font.size=Pt(16) wb1.close()#關閉excel file_dir =os.path.split(file1)[0] oldfname = os.path.split(file1)[1][:-5] newfname =file_dir +"\\" + oldfname + '.py.docx' Doc.save(newfname)#保存數據 showinfo('提示','新建成功') #檢查文件為xlsx def checkxlsx(file): f = file[-5:] if f != '.xlsx': showinfo('錯誤','文件擴展名必須是xlsx') else: convxlsx(file) #獲取拖放的文件名 def dragged_files(files): msg = '\n'.join((item.decode('gbk') for item in files)) #showinfo('您拖放的文件',msg) file= files[0].decode('gbk') checkxlsx(file) #創建root窗口 root = Tk()# 初始化 root.title("xlsx轉doc,請將xlsx文件拖放到窗體,將在同目錄下生成docx文檔") root.geometry('600x200+100+100') #長x寬+左上角x和y windnd.hook_dropfiles(root,func=dragged_files) root.mainloop()