python實現 xlsx多檔案合併

2021-09-26 08:31:24 字數 1809 閱讀 3660

#多xlsx檔案的合併,xlsx和xls檔案都可

# -*- coding:utf-8 -*-

import xlrd,xlsxwriter

#設定要合併的所有檔案

allxls=[r"c:/users/cn/jupyter file/test1.xlsx",r"c:/users/cn/jupyter file/test2.xlsx",r"c:/users/cn/jupyter file/test3.xlsx"]

#設定要合併到的檔案

end_xls=r"c:/users/cn/jupyter file/endxls.xlsx"

#開啟**

def open_xls(file):

try:

fh=xlrd.open_workbook(file)

return fh

except exception as e:

print(str("開啟出錯,錯誤為:"+e))

def open_xls(file):

try:

fh=xlrd.open_workbook(file)

return fh

except exception as e:

print("開啟檔案錯誤:"+e)

#定義乙個目標excel

endxls=xlsxwriter.workbook(end_xls)

all_sheet_value=

#print(all_sheet_value)

num=-1

sheet_index=-1

#將列表all_sheet_value的內容寫入目標excel

for sheet in all_sheet_value:

sheet_index+=1

end_xls_sheet=endxls.add_worksheet(sheet_name[sheet_index])

num+=1

num1=-1

for sheet1 in sheet:

for sheet2 in sheet1:

num1+=1

num2=-1

for sheet3 in sheet2:

num2+=1

#print(num,num1,num2,sheet3)

#在第num1行的第num2列寫入sheet3的內容

end_xls_sheet.write(num1,num2,sheet3)

endxls.close()

#合併多個xlsx檔案成csv檔案

# -*- coding:utf-8 -*-

import os

import pandas as pd

import numpy as np

dir = r"c:\users\cn\jupyter file\多檔案合併\test"#設定工作路徑

#新建列表,存放檔名(可以忽略,但是為了做的過程能心裡有數,先放上)

filename_excel =

#新建列表,存放每個檔案資料框(每乙個excel讀取後存放在資料框)

frames =

#檢視合併後的資料

result.head()

result.shape

result.to_csv(r'c:\users\cn\jupyter file\多檔案合併\a12.csv',encoding='utf_8_sig',sep=',',index = false)

#儲存合併的資料到c:\users\cn\jupyter file\多檔案合併資料夾中,並把合併後的檔案命名為a12.csv,解決中文亂碼問題encoding

python讀取xlsx檔案

我是在win7下讀取的。python版本是 3.5 import xlrd import re import sqlite3 def read xlsx workbook xlrd.open workbook e 20160322.xlsx booksheet workbook.sheet by n...

python讀取xlsx檔案

encoding utf 8 from openpyxl import load workbook workbook load workbook u xlsx 相對路徑,找到需要開啟的檔案位置 booksheet workbook.active 獲取當前活躍的 sheet,預設是第乙個 sheet ...

Python 讀取 xlsx檔案

讀取weibo.xlsx檔案 import pandas as pd df pd.read excel weibo.xlsx worksheet df.values for file in worksheet print file file 每一行 print file 0 每一行的第一列結果如下 ...