最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python將Word文檔導(dǎo)出為PDF格式并從Word文檔中提取數(shù)據(jù)

 更新時間:2025年11月26日 09:30:48   作者:諸神緘默不語  
在工作中,經(jīng)常會遇到需要把 Word 文檔轉(zhuǎn)換成 PDF 的情況,比如生成報表、分發(fā)文檔、或者做歸檔保存,PDF 格式在排版和跨平臺顯示上更穩(wěn)定,因此本文給大家介紹了如何使用Python將Word文檔導(dǎo)出為PDF格式并從Word文檔中提取數(shù)據(jù),需要的朋友可以參考下

1. 將Word文檔導(dǎo)出為PDF文檔

準(zhǔn)備環(huán)境

Python版本需要為3.8-3.14(pywin32的要求)
通過pip下載pywin32:

pip install pywin32

復(fù)制即可運(yùn)行的完整代碼

import os
from win32com import client as wc

def convert_word_to_pdf(word_path: str) -> str:
    """
    將單個Word文檔(doc/docx)轉(zhuǎn)換為PDF格式,并返回轉(zhuǎn)換后的文件路徑
    
    Args:
        word_path (str): Word文件路徑
        
    Returns:
        str: 轉(zhuǎn)換后的PDF文件路徑
        
    Raises:
        FileNotFoundError: 如果文件不存在
        ValueError: 如果文件不是Word格式或路徑無效
        Exception: 轉(zhuǎn)換過程中的其他錯誤
    """
    # 參數(shù)驗證
    if not word_path or not isinstance(word_path, str):
        raise ValueError("文件路徑不能為空且必須是字符串")
    
    # 檢查文件是否存在
    if not os.path.exists(word_path):
        raise FileNotFoundError(f"文件不存在: {word_path}")
    
    # 檢查文件擴(kuò)展名
    valid_extensions = ('.doc', '.docx')
    if not word_path.lower().endswith(valid_extensions):
        raise ValueError(f"文件不是Word格式(.doc/.docx): {word_path}")
    
    # 檢查文件是否被占用(臨時文件)
    if os.path.basename(word_path).startswith('~'):
        raise ValueError(f"文件可能是臨時文件: {word_path}")
    
    # 檢查文件大小(避免處理空文件或損壞文件)
    file_size = os.path.getsize(word_path)
    if file_size == 0:
        raise ValueError(f"文件為空: {word_path}")
    
    word = None
    doc = None
    
    try:
        # 創(chuàng)建Word應(yīng)用實例
        word = wc.Dispatch("Word.Application")
        word.Visible = False
        
        # 打開文檔
        doc = word.Documents.Open(word_path)
        
        # 生成新的PDF文件路徑
        base_path = os.path.splitext(word_path)[0]
        new_path = base_path + ".pdf"
        
        # 處理文件名沖突
        count = 0
        while os.path.exists(new_path):
            count += 1
            new_path = f"{base_path}({count}).pdf"
        
        # 保存為PDF格式(17代表PDF格式)
        doc.SaveAs(new_path, 17)
        
        # 驗證轉(zhuǎn)換后的文件
        if not os.path.exists(new_path):
            raise Exception("轉(zhuǎn)換后的PDF文件未創(chuàng)建成功")
        
        # 檢查轉(zhuǎn)換后的文件大小
        if os.path.getsize(new_path) == 0:
            raise Exception("轉(zhuǎn)換后的PDF文件為空")
        
        return new_path
        
    except Exception as e:
        # 清理可能創(chuàng)建的部分文件
        if 'new_path' in locals() and os.path.exists(new_path):
            try:
                os.remove(new_path)
            except:
                pass
        raise e
        
    finally:
        # 確保資源被正確釋放
        try:
            if doc:
                doc.Close()
        except:
            pass
            
        try:
            if word:
                word.Quit()
        except:
            pass


def main():
    """主函數(shù):轉(zhuǎn)換單個文件并顯示結(jié)果"""
    # 直接在代碼中設(shè)置要轉(zhuǎn)換的文件路徑
    word_file_path = r"D:\word_files\example.docx"  # 修改為您要轉(zhuǎn)換的Word文件路徑
    
    try:
        print(f"開始轉(zhuǎn)換文件: {word_file_path}")
        
        # 轉(zhuǎn)換文件
        pdf_path = convert_word_to_pdf(word_file_path)
        
        # 輸出成功信息
        print("\n" + "="*50)
        print("? 文件轉(zhuǎn)換成功!")
        print(f"原始文件: {word_file_path}")
        print(f"轉(zhuǎn)換后文件: {pdf_path}")
        print(f"文件大小: {os.path.getsize(pdf_path)} 字節(jié)")
        print("="*50)
        
    except FileNotFoundError as e:
        print(f"? 錯誤: {e}")
        print("請檢查文件路徑是否正確")
    except ValueError as e:
        print(f"? 錯誤: {e}")
        print("請確保文件是有效的Word文檔(.doc/.docx)")
    except Exception as e:
        print(f"? 轉(zhuǎn)換失敗: {e}")
        print("可能是Word應(yīng)用問題或文件損壞")


if __name__ == "__main__":
    main()

將代碼復(fù)制到你的Python編輯器中,并修改D:\word_files\example.docx為您需要轉(zhuǎn)換的Word文件路徑即可。轉(zhuǎn)換后的PDF文件與Word文件同名(如果已經(jīng)存在了同名PDF文件,將加上(count)后綴以避免沖突)

核心功能代碼

Word文檔導(dǎo)出為PDF的核心功能代碼為:

import os
from win32com import client as wc

word = wc.Dispatch("Word.Application")
word.Visible = False

doc = word.Documents.Open(word_path)
doc.SaveAs(new_path, 17)
doc.Close()

word.Quit()

將word_path對應(yīng)的Word文檔轉(zhuǎn)換為new_path對應(yīng)的PDF文檔。支持doc和docx格式的Word文檔。

doc.SaveAs(file_name, file_format)中file_format這個參數(shù)表示另存為文檔的格式。

  • 17: wdFormatPDF - PDF格式
  • 0: wdFormatDocument - Microsoft Office Word 97-2003文檔格式(.doc)
  • 16: wdFormatDocumentDefault - Word默認(rèn)文檔格式(.docx)
  • 7: wdFormatPDF - PDF格式(與17相同)
  • 8: wdFormatXPS - XPS格式

2. 如何用Python從Word中提取數(shù)據(jù):以處理簡歷為例

這個簡單案例的數(shù)據(jù)是隨機(jī)生成的10個格式相同的簡歷:

在實際工作中遇到的簡歷形式會更加多樣化,需要根據(jù)實際情況來進(jìn)行分析,甚至可能需要加入適當(dāng)智能能力。我在此提供的只是一個簡單樣例,執(zhí)行代碼會解析目錄下的所有Word格式簡歷:從表格中提取基本信息和教育背景,基本信息從表格中指定元素的位置獲取,教育背景從表格中逐行獲??;工作技能、技能特長、自我評價根據(jù)特定小標(biāo)題和段落格式獲取,都基本根據(jù)上圖中這種格式來進(jìn)行獲取。最后的解析結(jié)果會保存到CSV文件中(CSV文件可以用Excel或WPS表格直接打開)。
如果讀者在實際工作中遇到了更復(fù)雜的需求,也可以通過留言或私聊的方式咨詢我獲得答疑。
本案例生成測試數(shù)據(jù)的代碼已經(jīng)放在了下文中。由于是隨機(jī)生成的,所以每次生成的數(shù)據(jù)都是不同的。如果您想要在本次案例中的數(shù)據(jù),可以私聊我獲取文件壓縮包。

準(zhǔn)備環(huán)境

通過pip下載python-docx:

pip install python-docx

準(zhǔn)備測試數(shù)據(jù)

通過pip下載faker:

pip install faker

執(zhí)行代碼(簡歷文件會下載到工作路徑下的resumes文件夾下):
(如果您需要執(zhí)行這一部分代碼的話,還需要注意的是,生成的Word文件默認(rèn)使用中文宋體+英文Times New Roman格式,一般以中文為默認(rèn)語言的Windows電腦中都已內(nèi)置了這兩種字體,如果您的電腦設(shè)置為其它語言需要注意)

import os
import random
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn

from faker import Faker

def set_document_font(doc):
    """
    設(shè)置文檔的默認(rèn)字體為宋體和Times New Roman
    """
    # 設(shè)置全局樣式
    style = doc.styles['Normal']
    font = style.font
    font.name = 'Times New Roman'
    font._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
    font.size = Pt(12)

def set_paragraph_font(paragraph, bold=False, size=12):
    """
    設(shè)置段落的字體
    """
    for run in paragraph.runs:
        run.font.name = 'Times New Roman'
        run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
        run.font.size = Pt(size)
        run.font.bold = bold

def generate_resume_docx(filename, resume_data):
    """
    生成單個簡歷Word文檔
    
    Args:
        filename (str): 保存的文件名
        resume_data (dict): 簡歷數(shù)據(jù)
    """
    doc = Document()
    
    # 設(shè)置文檔字體
    set_document_font(doc)
    
    # 添加標(biāo)題
    title = doc.add_heading('個人簡歷', 0)
    title.alignment = WD_ALIGN_PARAGRAPH.CENTER
    set_paragraph_font(title, bold=True, size=16)
    
    # 添加個人信息表格
    doc.add_heading('基本信息', level=1)
    table = doc.add_table(rows=6, cols=4)
    table.style = 'Table Grid'
    
    # 填充基本信息表格
    info_cells = [
        ("姓名", resume_data['name']),
        ("性別", resume_data['gender']),
        ("年齡", str(resume_data['age'])),
        ("聯(lián)系電話", resume_data['phone']),
        ("郵箱", resume_data['email']),
        ("現(xiàn)居地", resume_data['address'])
    ]
    
    for i, (label, value) in enumerate(info_cells):
        table.cell(i, 0).text = label
        table.cell(i, 1).text = value
        table.cell(i, 2).text = "求職意向" if i == 0 else ""
        table.cell(i, 3).text = resume_data['job_intention'] if i == 0 else ""
        
        # 設(shè)置表格單元格字體
        for j in range(4):
            for paragraph in table.cell(i, j).paragraphs:
                set_paragraph_font(paragraph, bold=(j == 0 or j == 2))
    
    # 教育背景
    doc.add_heading('教育背景', level=1)
    edu_table = doc.add_table(rows=2, cols=4)
    edu_table.style = 'Table Grid'
    
    # 表頭
    headers = ["時間", "學(xué)校", "專業(yè)", "學(xué)歷"]
    for i, header in enumerate(headers):
        edu_table.cell(0, i).text = header
        for paragraph in edu_table.cell(0, i).paragraphs:
            set_paragraph_font(paragraph, bold=True)
    
    # 教育信息
    edu_table.cell(1, 0).text = resume_data['education']['period']
    edu_table.cell(1, 1).text = resume_data['education']['school']
    edu_table.cell(1, 2).text = resume_data['education']['major']
    edu_table.cell(1, 3).text = resume_data['education']['degree']
    
    # 設(shè)置教育信息行的字體
    for i in range(4):
        for paragraph in edu_table.cell(1, i).paragraphs:
            set_paragraph_font(paragraph)
    
    # 工作經(jīng)歷
    doc.add_heading('工作經(jīng)歷', level=1)
    for exp in resume_data['work_experience']:
        p = doc.add_paragraph()
        company_run = p.add_run(f"{exp['company']} | ")
        company_run.bold = True
        position_run = p.add_run(f"{exp['position']} | ")
        position_run.bold = True
        period_run = p.add_run(exp['period'])
        period_run.bold = True
        
        set_paragraph_font(p, bold=True)
        
        desc_para = doc.add_paragraph(exp['description'])
        set_paragraph_font(desc_para)
    
    # 技能特長
    doc.add_heading('技能特長', level=1)
    skills_para = doc.add_paragraph()
    for skill in resume_data['skills']:
        skills_para.add_run(f"? {skill}\n")
    set_paragraph_font(skills_para)
    
    # 自我評價
    doc.add_heading('自我評價', level=1)
    self_eval_para = doc.add_paragraph(resume_data['self_evaluation'])
    set_paragraph_font(self_eval_para)
    
    # 保存文檔
    doc.save(filename)
    print(f"已生成簡歷: {filename}")

def generate_sample_resumes(num=10):
    """
    生成指定數(shù)量的模擬簡歷
    
    Args:
        num (int): 簡歷數(shù)量,默認(rèn)為10
    """
    fake = Faker('zh_CN')
    
    # 創(chuàng)建保存目錄 - 使用os.path.join處理路徑
    resume_dir = os.path.join('resumes')
    os.makedirs(resume_dir, exist_ok=True)
    
    # 職位列表
    jobs = ['軟件工程師', '數(shù)據(jù)分析師', '產(chǎn)品經(jīng)理', 'UI設(shè)計師', '市場營銷', '人力資源', '財務(wù)專員', '運(yùn)營專員']
    
    # 技能列表
    skill_sets = {
        '軟件工程師': ['Python', 'Java', 'SQL', 'Linux', 'Git', 'Docker'],
        '數(shù)據(jù)分析師': ['Python', 'SQL', 'Excel', 'Tableau', '統(tǒng)計學(xué)', '機(jī)器學(xué)習(xí)'],
        '產(chǎn)品經(jīng)理': ['Axure', 'Visio', '項目管理', '需求分析', '用戶研究', 'PRD編寫'],
        'UI設(shè)計師': ['Photoshop', 'Sketch', 'Figma', 'Illustrator', '用戶體驗設(shè)計', '交互設(shè)計'],
        '市場營銷': ['市場分析', '營銷策劃', '社交媒體運(yùn)營', '內(nèi)容創(chuàng)作', '數(shù)據(jù)分析', '品牌管理'],
        '人力資源': ['招聘', '培訓(xùn)', '績效管理', '員工關(guān)系', 'HR系統(tǒng)', '勞動法'],
        '財務(wù)專員': ['會計', '財務(wù)報表', '稅務(wù)', '成本控制', '財務(wù)分析', 'ERP系統(tǒng)'],
        '運(yùn)營專員': ['內(nèi)容運(yùn)營', '用戶運(yùn)營', '活動策劃', '數(shù)據(jù)分析', '社交媒體', 'SEO/SEM']
    }
    
    # 生成簡歷
    for i in range(num):
        # 隨機(jī)選擇一個職位
        job = random.choice(jobs)
        
        # 生成簡歷數(shù)據(jù)
        resume_data = {
            'name': fake.name(),
            'gender': random.choice(['男', '女']),
            'age': random.randint(22, 35),
            'phone': fake.phone_number(),
            'email': fake.email(),
            'address': fake.city(),
            'job_intention': job,
            'education': {
                'period': f"{fake.year()}-{fake.year()}",
                'school': fake.company() + "大學(xué)",
                'major': fake.job() + "專業(yè)",
                'degree': random.choice(['本科', '碩士', '博士'])
            },
            'work_experience': [],
            'skills': random.sample(skill_sets[job], random.randint(4, 6)),
            'self_evaluation': fake.paragraph(nb_sentences=3)
        }
        
        # 生成工作經(jīng)歷
        num_experiences = random.randint(1, 3)
        for j in range(num_experiences):
            start_year = 2020 - j * 2
            resume_data['work_experience'].append({
                'company': fake.company(),
                'position': job,
                'period': f"{start_year}-{start_year+2}",
                'description': fake.paragraph(nb_sentences=2)
            })
        
        # 生成文檔 - 使用os.path.join處理路徑
        safe_name = resume_data['name'].replace(' ', '_')
        filename = os.path.join(resume_dir, f"簡歷_{safe_name}_{job}.docx")
        generate_resume_docx(filename, resume_data)

if __name__ == "__main__":
    print("開始生成模擬簡歷...")
    generate_sample_resumes(10)
    print("簡歷生成完成!")

復(fù)制即可運(yùn)行的完整代碼

簡歷Word文檔在resumes文件夾中,生成的resumes_data.csv就直接在工作路徑下:

import os
import re
import csv
from docx import Document

def extract_resume_info(filepath):
    """
    從Word簡歷中提取信息
    
    Args:
        filepath (str): Word文檔路徑
        
    Returns:
        dict: 提取的簡歷信息
    """
    try:
        doc = Document(filepath)
        resume_data = {
            'filename': os.path.basename(filepath),
            'name': '',
            'gender': '',
            'age': '',
            'phone': '',
            'email': '',
            'address': '',
            'job_intention': '',
            'education_period': '',
            'education_school': '',
            'education_major': '',
            'education_degree': '',
            'work_experience': '',
            'skills': '',
            'self_evaluation': ''
        }
        
        # 提取基本信息
        basic_info_extracted = False
        education_extracted = False
        in_work_experience = False
        in_skills = False
        in_self_evaluation = False
        
        work_experiences = []
        skills_list = []
        
        for i, paragraph in enumerate(doc.paragraphs):
            text = paragraph.text.strip()
            
            # 跳過空段落
            if not text:
                continue
                
            # 提取基本信息表格
            if text == '基本信息' and not basic_info_extracted:
                # 查找所有表格
                for table in doc.tables:
                    # 檢查表格是否包含基本信息
                    try:
                        if table.cell(0, 0).text == '姓名':
                            resume_data['name'] = table.cell(0, 1).text
                            resume_data['gender'] = table.cell(1, 1).text
                            resume_data['age'] = table.cell(2, 1).text
                            resume_data['phone'] = table.cell(3, 1).text
                            resume_data['email'] = table.cell(4, 1).text
                            resume_data['address'] = table.cell(5, 1).text
                            resume_data['job_intention'] = table.cell(0, 3).text
                            basic_info_extracted = True
                            break
                    except:
                        continue
                
                if not basic_info_extracted:
                    # 如果沒有找到表格,嘗試從段落中提取
                    extract_basic_info_from_text(doc, resume_data)
                    basic_info_extracted = True
            
            # 提取教育背景
            elif text == '教育背景' and not education_extracted:
                # 查找教育背景表格
                for table in doc.tables:
                    try:
                        if table.cell(0, 0).text == '時間' and '學(xué)校' in table.cell(0, 1).text:
                            resume_data['education_period'] = table.cell(1, 0).text
                            resume_data['education_school'] = table.cell(1, 1).text
                            resume_data['education_major'] = table.cell(1, 2).text
                            resume_data['education_degree'] = table.cell(1, 3).text
                            education_extracted = True
                            break
                    except:
                        continue
            
            # 提取工作經(jīng)歷
            elif text == '工作經(jīng)歷':
                in_work_experience = True
                continue
            elif in_work_experience and text and not text.startswith('技能特長') and not text.startswith('自我評價'):
                # 檢查是否是工作經(jīng)歷標(biāo)題(包含公司、職位、時間,用 | 分隔)
                if ' | ' in text:
                    parts = text.split(' | ')
                    if len(parts) >= 3:
                        # 提取公司、職位和時間
                        company = parts[0].strip()
                        position = parts[1].strip()
                        period = parts[2].strip()
                        
                        work_experiences.append({
                            'company': company,
                            'position': position,
                            'period': period
                        })
                # 否則,可能是工作描述,但我們主要關(guān)注標(biāo)題信息
            
            # 提取技能特長
            elif text == '技能特長':
                in_skills = True
                in_work_experience = False
                continue
            elif in_skills and text and not text.startswith('自我評價'):
                # 技能特長部分是以 ? 開頭的列表項
                lines = text.split('\n')
                for line in lines:
                    line = line.strip()
                    if line.startswith('?'):
                        skill = line[1:].strip()  # 移除 ? 符號
                        if skill:
                            skills_list.append(skill)
            
            # 提取自我評價
            elif text == '自我評價':
                in_self_evaluation = True
                in_skills = False
                continue
            elif in_self_evaluation and text:
                resume_data['self_evaluation'] = text
                in_self_evaluation = False
        
        # 如果沒有提取到基本信息,嘗試其他方法
        if not basic_info_extracted:
            extract_basic_info_from_text(doc, resume_data)
        
        # 將工作經(jīng)歷和技能列表轉(zhuǎn)換為字符串
        if work_experiences:
            work_exp_str = " | ".join([
                f"{exp['company']} ({exp['position']}, {exp['period']})" 
                for exp in work_experiences
            ])
            resume_data['work_experience'] = work_exp_str
        
        if skills_list:
            resume_data['skills'] = "; ".join(skills_list)
        
        # 清理數(shù)據(jù)
        clean_resume_data(resume_data)
        
        return resume_data
        
    except Exception as e:
        print(f"提取簡歷信息時出錯 {filepath}: {e}")
        return None

def extract_basic_info_from_text(doc, resume_data):
    """
    從文檔文本中提取基本信息
    """
    for paragraph in doc.paragraphs:
        text = paragraph.text
        
        # 提取姓名
        if not resume_data['name'] and len(text) <= 4 and not any(keyword in text for keyword in ['基本信息', '教育背景', '工作經(jīng)歷']):
            resume_data['name'] = text
        
        # 提取電話
        phone_match = re.search(r'1[3-9]\d{9}', text)
        if phone_match and not resume_data['phone']:
            resume_data['phone'] = phone_match.group()
        
        # 提取郵箱
        email_match = re.search(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text)
        if email_match and not resume_data['email']:
            resume_data['email'] = email_match.group()

def clean_resume_data(resume_data):
    """
    清理提取的簡歷數(shù)據(jù)
    """
    # 清理年齡,只保留數(shù)字
    if resume_data['age']:
        age_match = re.search(r'\d+', resume_data['age'])
        if age_match:
            resume_data['age'] = age_match.group()

def save_to_csv(resumes, csv_filename='resumes_data.csv'):
    """
    將簡歷數(shù)據(jù)保存為CSV文件
    
    Args:
        resumes (list): 簡歷數(shù)據(jù)列表
        csv_filename (str): CSV文件名
    """
    if not resumes:
        print("沒有簡歷數(shù)據(jù)可保存")
        return
    
    # 定義CSV列名
    fieldnames = [
        'filename', 'name', 'gender', 'age', 'phone', 'email', 'address', 
        'job_intention', 'education_period', 'education_school', 'education_major', 
        'education_degree', 'work_experience', 'skills', 'self_evaluation'
    ]
    
    with open(csv_filename, 'w', newline='', encoding='utf-8-sig') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        
        for resume in resumes:
            writer.writerow(resume)
    
    print(f"簡歷數(shù)據(jù)已保存到: {csv_filename}")

def analyze_resumes(directory='resumes'):
    """
    分析目錄中的所有簡歷
    
    Args:
        directory (str): 簡歷目錄路徑
    """
    if not os.path.exists(directory):
        print(f"目錄不存在: {directory}")
        return
    
    resumes = []
    
    # 遍歷目錄中的所有Word文檔
    for filename in os.listdir(directory):
        if filename.endswith('.docx'):
            filepath = os.path.join(directory, filename)
            print(f"正在處理: {filename}")
            
            resume_data = extract_resume_info(filepath)
            if resume_data:
                resumes.append(resume_data)
    
    # 輸出分析結(jié)果
    print("\n" + "="*50)
    print("簡歷分析結(jié)果")
    print("="*50)
    
    # 統(tǒng)計基本信息
    print(f"共處理簡歷: {len(resumes)} 份")
    
    if not resumes:
        return
    
    # 按職位意向統(tǒng)計
    job_counts = {}
    for resume in resumes:
        job = resume.get('job_intention', '未知')
        job_counts[job] = job_counts.get(job, 0) + 1
    
    print("\n職位意向分布:")
    for job, count in job_counts.items():
        print(f"  {job}: {count} 人")
    
    # 平均年齡
    ages = []
    for resume in resumes:
        if resume['age'] and resume['age'].isdigit():
            ages.append(int(resume['age']))
    if ages:
        avg_age = sum(ages) / len(ages)
        print(f"\n平均年齡: {avg_age:.1f} 歲")
    
    # 學(xué)歷分布
    degree_counts = {}
    for resume in resumes:
        degree = resume.get('education_degree', '未知')
        degree_counts[degree] = degree_counts.get(degree, 0) + 1
    
    print("\n學(xué)歷分布:")
    for degree, count in degree_counts.items():
        print(f"  {degree}: {count} 人")
    
    # 工作經(jīng)歷統(tǒng)計
    work_exp_counts = {}
    for resume in resumes:
        work_exp = resume.get('work_experience', '')
        if work_exp:
            # 計算工作經(jīng)歷數(shù)量(通過 | 分隔符)
            count = work_exp.count('|') + 1
            work_exp_counts[count] = work_exp_counts.get(count, 0) + 1
    
    print("\n工作經(jīng)歷數(shù)量分布:")
    for count, freq in sorted(work_exp_counts.items()):
        print(f"  {count} 段經(jīng)歷: {freq} 人")
    
    # 技能統(tǒng)計
    skill_counts = {}
    for resume in resumes:
        skills_str = resume.get('skills', '')
        if skills_str:
            # 按分號分隔技能
            skills = [skill.strip() for skill in skills_str.split(';') if skill.strip()]
            for skill in skills:
                skill_counts[skill] = skill_counts.get(skill, 0) + 1
    
    # 取前10個最常用技能
    top_skills = sorted(skill_counts.items(), key=lambda x: x[1], reverse=True)[:10]
    print("\n最常用技能(前10):")
    for skill, count in top_skills:
        print(f"  {skill}: {count} 次")
    
    # 保存詳細(xì)數(shù)據(jù)到CSV文件
    save_to_csv(resumes, 'resumes_data.csv')
    
    print(f"\n詳細(xì)數(shù)據(jù)已保存到: resumes_data.csv")

if __name__ == "__main__":
    print("開始提取簡歷信息...")
    analyze_resumes()
    print("簡歷分析完成!")

生成的resumes_data.csv為:

在生成過程中還會在終端輸出:

開始提取簡歷信息...
正在處理: 簡歷_劉桂花_數(shù)據(jù)分析師.docx
正在處理: 簡歷_吳婷_數(shù)據(jù)分析師.docx
正在處理: 簡歷_吳建平_UI設(shè)計師.docx
正在處理: 簡歷_孫婷_軟件工程師.docx
正在處理: 簡歷_張麗麗_市場營銷.docx
正在處理: 簡歷_李建軍_運(yùn)營專員.docx
正在處理: 簡歷_歐寧_數(shù)據(jù)分析師.docx
正在處理: 簡歷_王利_運(yùn)營專員.docx
正在處理: 簡歷_羅歡_運(yùn)營專員.docx
正在處理: 簡歷_韓巖_財務(wù)專員.docx

==================================================
簡歷分析結(jié)果
==================================================
共處理簡歷: 10 份

職位意向分布:
  數(shù)據(jù)分析師: 3 人
  UI設(shè)計師: 1 人
  軟件工程師: 1 人
  市場營銷: 1 人
  運(yùn)營專員: 3 人
  財務(wù)專員: 1 人

平均年齡: 28.3 歲

學(xué)歷分布:
  博士: 4 人
  碩士: 3 人
  本科: 3 人

工作經(jīng)歷數(shù)量分布:
  1 段經(jīng)歷: 2 人
  2 段經(jīng)歷: 4 人
  3 段經(jīng)歷: 4 人

最常用技能(前10):
  數(shù)據(jù)分析: 4 次
  Excel: 3 次
  機(jī)器學(xué)習(xí): 3 次
  SQL: 3 次
  Python: 3 次
  用戶運(yùn)營: 3 次
  活動策劃: 3 次
  SEO/SEM: 3 次
  Tableau: 2 次
  統(tǒng)計學(xué): 2 次
簡歷數(shù)據(jù)已保存到: resumes_data.csv

詳細(xì)數(shù)據(jù)已保存到: resumes_data.csv
簡歷分析完成!

核心功能代碼

python-docx包

引入環(huán)境:from docx import Document
初始化文檔對象:doc=Document(filepath)
Document對象就是一個Word文檔對象,由一個個段落(paragraph)組成。paragraph中的屬性text就是文本(字符串)
從Document對象中,也可以通過tables屬性獲得表格列表,表格是Table對象。表格可以通過cell(行,列)(行列數(shù)都從0開始)獲取單元格,每個單元格的屬性text就是文本(字符串)

csv包

初始化寫入對象:

writer = csv.DictWriter(csv文件流, fieldnames=列名列表)
writer.writeheader()

寫入一行:writer.writerow(dict) 傳入字典參數(shù),以列名為鍵,值會寫入CSV

os包

  1. 從整個文件路徑里獲取文件名:os.path.basename(filepath)
  2. 獲取文件夾下所有文件的文件名:os.listdir(directory)
  3. 將文件夾路徑和文件名組合為文件路徑:os.path.join(directory, filename)(事實上這個函數(shù)可以疊好幾層路徑,可以從父文件夾疊子文件夾名再疊文件名這樣疊成完整路徑)

re包(正則表達(dá)式)

對正則表達(dá)式的詳細(xì)介紹不在本專欄涉及的范圍中。

re.search(pattern,text):搜索文本中第一個符合指定正則表達(dá)式pattern的文本,返回值的group()函數(shù)返回匹配的字符串

  1. phone_match = re.search(r'1[3-9]\d{9}', text) 匹配中國大陸手機(jī)號碼格式
    總長度:11位數(shù)字
    第一位:必須是1
    第二位:必須是3-9(排除12開頭的特殊號碼)
    后面9位:任意數(shù)字
  2. email_match = re.search(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text) 匹配標(biāo)準(zhǔn)郵箱格式:用戶名@域名.頂級域名
    用戶名:字母、數(shù)字、特殊字符
    域名:字母、數(shù)字、點(diǎn)、減號
    頂級域名:至少2個字母

Python對象處理

字符串處理

  1. strip():去除文本開頭與結(jié)尾的空格、回車等控制符
  2. startswith(str):文本開頭是否是特定字符串
  3. split(分隔符):將文本用分隔符切分開。如果不顯式設(shè)置分隔符,默認(rèn)用空格、回車等控制符來切分
  4. join(list):將字符串列表組合成一個字符串,用對象文本作為連接詞
  5. isdigit():如果字符串中所有字符都是數(shù)字,返回True

容器對象處理

  1. 列表
    1. 直接通過索引切片([index])獲取對象
    2. append(obj):添加一個對象到列表末尾
  2. 字典

通過鍵值對格式可以直接創(chuàng)建字典對象:

{
    'company': company,
    'position': position,
    'period': period
}
  1. 通過鍵名切片可以直接獲取值和賦值(如果鍵名不存在,會直接創(chuàng)建鍵值對),如resume_data['self_evaluation']
  2. 通過get(key,default_value) 獲取值,在鍵(key)不存在時可以返回一個默認(rèn)值(default_value
  3. any(obj)如果obj中任何一個元素為True,就返回True

以上就是使用Python將Word文檔導(dǎo)出為PDF格式并從Word文檔中提取數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于Python Word導(dǎo)出為PDF并提取數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

瓮安县| 兰州市| 天镇县| 和政县| 游戏| 卫辉市| 景东| 商洛市| 崇文区| 新营市| 阳城县| 曲松县| 蒲江县| 遂昌县| 赞皇县| 宜川县| 中牟县| 饶阳县| 会同县| 海兴县| 石渠县| 庆安县| 邵阳市| 南投市| 林甸县| 南丹县| 莎车县| 上栗县| 重庆市| 凌源市| 焦作市| 利川市| 留坝县| 中超| 海门市| 阳东县| 礼泉县| 邯郸县| 朝阳县| 建湖县| 达拉特旗|