python自动化之 excel转word
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()