|

楼主 |
发表于 2021-3-29 19:10:31
|
显示全部楼层
代码如下:
- from docx import Document
- import openpyxl
- #打开excel文件
- filename = "文件.xlsx"
- wb = openpyxl.load_workbook(filename=filename)
- ws = wb.active
- #1.创建date列表
- date_list = []
- for row in ws.iter_rows(min_row=2,min_col=6,max_col=6):
- for cell in row:
- #判断 日期是否在date列表中&cell是否为空,如不在date列表中,则将其添加到date列表
- if cell.value not in date_list and cell.value is not None:
- date_list.append(cell.value)
- else:
- continue
- print(date_list)
- #2.根据date列表,依次获取某一天的内容
- for date in date_list:
- #content汇总
- content = ""
- #获取当天的内容
- for each_row in ws.iter_rows(min_row=2,min_col=2,max_col=9):
- if each_row[4].value == date:
- str1 = each_row[7].value.replace("\n\n","\n").replace("_x000D_","") + "\n" + "(" + each_row[0].value + ")" + "\n"
- content += str1
- #3.将当天的content写入word中,并保存
- doc = Document("模板.docx")
- tables = doc.tables
- tables[0].cell(2,1).text = content
- doc_name = date + ".docx"
- doc.save(doc_name)
复制代码
可是我看word上关于单元格的操作就叫两端对齐,没有左对齐的格式
q1.1
|
|