python-docx知识点(bilibili戴师兄python课程笔记)
pip install python-docx
from docx import python-docx
document = Document()
1.加标题
document.add_heading('一级标题填这', level=1)
document.add_heading('二级标题填这', level=2)
2.加正文
document.add_paragraph('正文写这')
3.导出docx文件
document.save('文件.docx')
4.项目:办公自动化
import pandas as pd
video_list = pd.read_excel('/Users/mathildalin/Desktop/video_list.xlsx')
speech_text = pd.read_excel('/Users/mathildalin/Desktop/speech_text.xlsx')
video_list['AwemeId'] = video_list['AwemeId'].astype(str)
speech_text['VideoId'] = speech_text['VideoId'].astype(str)
merge = pd.merge(video_list,speech_text, how='inner',left_on = 'AwemeId',right_on='VideoId')
merge
document = Document()
for i in range(len(merge)):
if merge.iloc[i]['品牌'] != merge.iloc[i-1]['品牌'] or i == 0:
document.add_heading(merge.iloc[i]['品牌'],level=1)
document.add_heading(merge.iloc[i]['视频标题'],level=2)
document.add_paragraph(f'达人昵称:{merge.iloc[i]["BloggerName"]}')
document.add_paragraph(f'视频地址:douyin.com/video/{merge.iloc[i]["AwemeId"]}')
document.add_paragraph(merge.iloc[i]['视频文案'])
document.save('demo.docx')