精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
編程語言Python 操作 Word

Python 操作 Word

用 docx 模塊讀取 Word

docx 安裝

cmd 中輸入pip install python-docx 即可安裝 docx 模塊

docx 常用函數

創建空白文檔

from docx import Document

document = Document()document.save("word.docx")  # 生成空白 wordprint(document)

讀取文檔

document = Document("word.docx")  # 讀取現有的 word 建立文檔對象

獲取文檔段落

from docx import Document

document = Document("word.docx")  # 讀取現有的 word 對象all_paragraphs = document.paragraphsprint(type(all_paragraphs))for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的樣式名稱
    # 打印每一個段落的文字
    print(paragraph.text)
    # 循環讀取每個段落里的run內容# 一個run對象是相同樣式文本的延續for paragraph in all_paragraphs:
    for run in paragraph.runs:
        print(run.text)  # 打印run內容

Word 調整樣式

from docx import Documentfrom docx.shared import Pt, RGBColor

document = Document()  # 讀取現有的 word 建立文檔對象# 二、寫入內容# 段落p1 = document.add_paragraph("早睡早起!!!")format_p1 = p1.paragraph_format# 左右縮進format_p1.left_indent = Pt(20)format_p1.right_indent = Pt(20)# 首行縮進format_p1.first_line_indent = Pt(20)# 行間距format_p1.line_spacing = 1# 追加# 一個run對象是相同樣式文本的延續run = p1.add_run("我也想做癩皮狗\n")# 字體,字號,文字顏色run.font.size = Pt(12)run.font.name = "微軟雅黑"run.font.color.rgb = RGBColor(235, 123, 10)run1 = p1.add_run("賈某人不學習")# 加粗,下劃線,斜體run1.bold = Truerun1.font.underline = Truerun1.font.italic = True# # 三、保存文件document.save("word.docx")all_paragraphs = document.paragraphs# print(type(all_paragraphs))#,打印后發現是列表# 是列表就開始循環讀取dfor paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的樣式名稱
    # 打印每一個段落的文字
    print(paragraph.text)
    # 循環讀取每個段落里的run內容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run內容

Word 寫入操作

from docx import Documentfrom docx.shared import Pt, RGBColor

document = Document()  # 讀取現有的 word 建立文檔對象# 二、寫入內容document.add_heading("python 操作 Word")# 段落p1 = document.add_paragraph("早睡早起!!!")p1.insert_paragraph_before("Power!!!")format_p1 = p1.paragraph_format# 左右縮進format_p1.left_indent = Pt(20)format_p1.right_indent = Pt(20)# 首行縮進format_p1.first_line_indent = Pt(20)# 行間距format_p1.line_spacing = 1# 追加# 一個run對象是相同樣式文本的延續run = p1.add_run("我也想做癩皮狗\n")# 字體,字號,文字顏色run.font.size = Pt(12)run.font.name = "微軟雅黑"run.font.color.rgb = RGBColor(235, 123, 10)run1 = p1.add_run("賈某人不學習")# 加粗,下劃線,斜體run1.bold = Truerun1.font.underline = Truerun1.font.italic = True# # 三、保存文件document.save("word.docx")all_paragraphs = document.paragraphs# print(type(all_paragraphs))#,打印后發現是列表# 是列表就開始循環讀取dfor paragraph in all_paragraphs:
    # print(paragraph.paragraph_format)  # 打印出word中每段的樣式名稱
    # 打印每一個段落的文字
    print(paragraph.text)
    # 循環讀取每個段落里的run內容
    # for run in paragraph.runs:
    # print(run.text)  # 打印run內容

本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
手把手教你用Python操縱Word自動編寫離職報告
python-docx處理Word必備工具
一篇就夠,python 操作 word 文檔,使用 python-docx 落地實現
【Python自動化辦公】實現excel表中的數據批量導入到word指定位置(表格形式和下滑線形式)
憐香惜玉,我用python幫助辦公室文秘
用 python 來操作 docx, xlsx 格式文件(二)(使用 docx 庫操作 docx 格式文件
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 泽州县| 绥阳县| 冕宁县| 樟树市| 阳江市| 浙江省| 伊吾县| 黄骅市| 澄迈县| 望奎县| 当雄县| 彭泽县| 延安市| 曲阜市| 阳城县| 通州市| 蒙自县| 绥中县| 广德县| 类乌齐县| 南阳市| 察隅县| 深水埗区| 山东省| 个旧市| 徐水县| 临沧市| 潼关县| 阳城县| 深州市| 江门市| 留坝县| 康定县| 甘德县| 石家庄市| 安泽县| 漳平市| 松江区| 缙云县| 娄底市| 阳朔县|