开心六月综合激情婷婷|欧美精品成人动漫二区|国产中文字幕综合色|亚洲人在线成视频

    1. 
      
        <b id="zqfy3"><legend id="zqfy3"><fieldset id="zqfy3"></fieldset></legend></b>
          <ul id="zqfy3"></ul>
          <blockquote id="zqfy3"><strong id="zqfy3"><dfn id="zqfy3"></dfn></strong></blockquote>
          <blockquote id="zqfy3"><legend id="zqfy3"></legend></blockquote>
          打開(kāi)APP
          userphoto
          未登錄

          開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

          開(kāi)通VIP
          10個(gè)Python Excel自動(dòng)化腳本

          1. 安裝必要的庫(kù)

          首先,確保你的演示版本與 pandas 和 openpyxl 這兩個(gè)庫(kù)相匹配。這兩個(gè)庫(kù)都是 Python 操作 Excel 的核心工具。

          pip install pandas openpyxl

          2. 讀取Excel文件

          最基本的操作,讀取Excel文件并顯示前幾行數(shù)據(jù)。

          import pandas as pddf = pd.read_excel('example.xlsx')print(df.head())  # 顯示前5行數(shù)據(jù)

          3. 寫(xiě)入Excel文件

          將數(shù)據(jù)保存到新的Excel文件中。

          df.to_excel('output.xlsx', index=False)  # index=False不保存索引列

          4. 合并多個(gè)工作表

          如果你需要Excel中的多個(gè)工作表,則這樣做:

          xls = pd.ExcelFile('multi_sheets.xlsx')dfs = {sheet_name: xls.parse(sheet_name) for sheet_name in xls.sheet_names}combined_df = pd.concat(dfs.values(), ignore_index=True)

          5.數(shù)據(jù)清理:刪除空值行

          快速清理數(shù)據(jù),移除其中含有的無(wú)效數(shù)據(jù)。

          df_cleaned = df.dropna() # 刪除所有含空值的行

          6. 數(shù)據(jù)篩選

          基于條件篩選數(shù)據(jù)。

          filtered_df = df[df['Sales'] > 10000]  # 篩選出銷(xiāo)售額大于10000的記錄

          7. 數(shù)據(jù)透視表

          使用Pandas輕松創(chuàng)建數(shù)據(jù)透視表。

          pivot_table = pd.pivot_table(df, values='Sales', index=['Category'], aggfunc=np.sum)

          8. 自動(dòng)化圖表生成

          使用matplotlib或plotly生成圖表并保存。

          import matplotlib.pyplot as pltplt.figure(figsize=(10,6))df.plot(kind='bar', x='Month', y='Sales')plt.title('Monthly Sales')plt.savefig('monthly_sales.png')

          9. 批量修改頁(yè)面布局

          雖然Pandas本身不支持樣式表,但可以借助openpyxl對(duì)已保存的Excel進(jìn)行進(jìn)一步美化。

          from openpyxl import load_workbookwb = load_workbook('output.xlsx')ws = wb.activefor row in ws.iter_rows(min_row=2, max_col=3, values_only=True): if row[2] > 5000: # 假設(shè)第三列是'Profit',大于5000標(biāo)紅 cell = ws.cell(row=row[0], column=3) cell.fill = PatternFill(start_color='FF0000', end_color='FF0000', fill_type='solid')wb.save('styled_output.xlsx')

          10. 自動(dòng)郵件發(fā)送Excel報(bào)告

          最后,自動(dòng)化工作的盡善盡美收尾——用Python發(fā)送帶有附件的郵件。

          import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email.mime.text import MIMETextfrom email.utils import COMMASPACEfrom email import encodersdef send_email(sender, recipients, subject, body, attachment_path):    msg = MIMEMultipart()    msg['From'] = sender    msg['To'] = COMMASPACE.join(recipients)    msg['Subject'] = subject    msg.attach(MIMEText(body))    part = MIMEBase('application', 'octet-stream')    with open(attachment_path, 'rb') as file:        part.set_payload(file.read())    encoders.encode_base64(part)    part.add_header('Content-Disposition', 'attachment; filename='%s'' % os.path.basename(attachment_path))    msg.attach(part)    server = smtplib.SMTP('smtp.example.com', 587)    server.starttls()    server.login('your_username', 'your_password')    server.sendmail(sender, recipients, msg.as_string())    server.quit()# 使用函數(shù)發(fā)送郵件send_email('you@example.com', ['colleague1@example.com', 'colleague2@example.com'],            'Monthly Sales Report', 'Please find attached the latest sales report.', 'monthly_sales.xlsx')

          以上就是本次分享的10個(gè)Python Excel自動(dòng)化腳本,涵蓋了數(shù)據(jù)讀取、清洗、分析、Visual Basic及報(bào)告自動(dòng)化發(fā)送的整個(gè)過(guò)程。掌握這些技巧,相信您代表辦公室里最閃耀的那顆星!?記得實(shí)踐是檢驗(yàn)真理的唯一標(biāo)準(zhǔn),試用吧!

          本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
          打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
          猜你喜歡
          類(lèi)似文章
          用Python 創(chuàng)建 Excel 高級(jí)工作表
          使用Pandas讀取復(fù)雜Excel表單
          多文件夾下Excel指定列的提取合并
          Python3操作excel的集大成者pandas
          python讀取excel文件的三種方法
          Python處理Excel工作表
          更多類(lèi)似文章 >>
          生活服務(wù)
          分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
          綁定賬號(hào)成功
          后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
          如果VIP功能使用有故障,
          可點(diǎn)擊這里聯(lián)系客服!

          聯(lián)系客服