如何使用 python 更改 excel 中的列格式
How to change column format in excel with python
我想用“openpyxl”方法将特定的行和列一个 sheet 复制到另一个。但我的主要 excel 文件是 .xlsb 文件,“openpyxl”不支持 .xlsb file.So 我构建的方式很复杂。 (* 根据公司规定,我无法更改 Microsoft Excel 的 .xlsb)。
main document.xlsb file->temporary document.xlsx->my analyze document.xlsx
-首先,我将数据格式 .xlsb 更改为 .xlsx pandas。
-之后,从临时 document.xlsx,我使用 openpyxl 方法获取特定的列和行并粘贴到我的分析 document.xlsx
-我的问题是:我想将 D 列格式从“通用”更改为“短期”,我是 Python 的初学者。你能帮我了解一下代码吗?
另外,如果我可以更改“.xlsb 到 .xlsx 转换期间”中的格式单元格,也许我可以从用户那里获取输入:“您想追加哪个日期 'my analyse document.xlsx?'”
'main document.xlsx'
'temporary document.xlsx'
'my analyse document.xlsx'
import pandas as pd
import openpyxl
df = pd.read_excel("main document.xlsb",sheet_name="Data", engine="pyxlsb")
df.to_excel("temporary document.xlsx")
#! Python 3
# - Copy and Paste Ranges using OpenPyXl library
# Prepare the spreadsheets to copy from and paste too.
# File to be copied
wb = openpyxl.load_workbook("temporary document.xlsx") # Add file name
sheet = wb["Sheet1"] # Add Sheet name
# File to be pasted into
template = openpyxl.load_workbook("my analyse document.xlsx") # Add file name
temp_sheet = template["Sheet2"] # Add Sheet name
# Copy range of cells as a nested list
# Takes: start cell, end cell, and sheet you want to copy from.
def copyRange(startCol, startRow, endCol, endRow, sheet):
rangeSelected = []
# Loops through selected Rows
for i in range(startRow, endRow + 1, 1):
# Appends the row to a RowSelected list
rowSelected = []
for j in range(startCol, endCol + 1, 1):
rowSelected.append(sheet.cell(row=i, column=j).value)
# Adds the RowSelected List and nests inside the rangeSelected
rangeSelected.append(rowSelected)
return rangeSelected
# Paste range
# Paste data from copyRange into template sheet
def pasteRange(startCol, startRow, endCol, endRow, sheetReceiving, copiedData):
countRow = 0
for i in range(startRow, endRow + 1, 1):
countCol = 0
for j in range(startCol, endCol + 1, 1):
sheetReceiving.cell(row=i, column=j).value = copiedData[countRow][countCol]
countCol += 1
countRow += 1
def createData():
print("Processing...")
selectedRange = copyRange(2, 2011, 183, 2274, sheet) # Change the 4 number values (startCol, startRow, endCol, endRow, sheet)
pastingRange = pasteRange(2, 4573, 182, 4836, temp_sheet, selectedRange) # Change the 4 number values (startCol, startRow, endCol, endRow, sheet)
# You can save the template as another file to create a new file here too.s
template.save("my analyse document.xlsx")
print("Range copied and pasted!")
go= createData()
是的,看看这里 docs:
import xlsxwriter
workbook = xlsxwriter.Workbook('your_file.xlsx')
worksheet = workbook.add_worksheet()
cell_format05 = workbook.add_format()
cell_format05.set_num_format('mm/dd/yy')
worksheet.write(5, 0, 36892.521, cell_format05) # output -> 01/01/01
# untested code for you, get your cells into column_D8_downwards array
# this is rather a structural code, not functional!
row = 0
for cell in column_D8_downwards:
worksheet.write(row, 'D8', cell, cell_format_05)
row=+1
workbook.close()
因此遍历列中的所有单元格(D8 向下)并将旧值写入新格式的单元格。
您也可以在 xlrd
的帮助下使用 pandas.DataFrame
读取数据后执行相同的操作:
import xlrd
import pandas as pd
df = pd.read_csv('Your_File.csv')
df = df[6:].reset_index()
df.columns = df.iloc[0]
df['Date'] = df['Date_Int'].apply(lambda x: xlrd.xldate.xldate_as_datetime(x, 0))
print(df)
TARİH Date
0 43891 2020-03-01
1 43892 2020-03-02
2 43893 2020-03-03
3 43894 2020-03-04
4 43895 2020-03-05
此外,您可以根据需要更改日期格式。
问题解决的很简单,解决的时候惊呆了
1-打开"my analyse document.xlsx"
2-Select整个"D"列
3-单击主页按钮
4-格式单元格-> 短日期
即使我用 python 更新 excel 数据,列格式类型也没有改变。
感谢大家的支持
我想用“openpyxl”方法将特定的行和列一个 sheet 复制到另一个。但我的主要 excel 文件是 .xlsb 文件,“openpyxl”不支持 .xlsb file.So 我构建的方式很复杂。 (* 根据公司规定,我无法更改 Microsoft Excel 的 .xlsb)。
main document.xlsb file->temporary document.xlsx->my analyze document.xlsx
-首先,我将数据格式 .xlsb 更改为 .xlsx pandas。
-之后,从临时 document.xlsx,我使用 openpyxl 方法获取特定的列和行并粘贴到我的分析 document.xlsx
-我的问题是:我想将 D 列格式从“通用”更改为“短期”,我是 Python 的初学者。你能帮我了解一下代码吗? 另外,如果我可以更改“.xlsb 到 .xlsx 转换期间”中的格式单元格,也许我可以从用户那里获取输入:“您想追加哪个日期 'my analyse document.xlsx?'”
'main document.xlsx'
'temporary document.xlsx'
'my analyse document.xlsx'
import pandas as pd
import openpyxl
df = pd.read_excel("main document.xlsb",sheet_name="Data", engine="pyxlsb")
df.to_excel("temporary document.xlsx")
#! Python 3
# - Copy and Paste Ranges using OpenPyXl library
# Prepare the spreadsheets to copy from and paste too.
# File to be copied
wb = openpyxl.load_workbook("temporary document.xlsx") # Add file name
sheet = wb["Sheet1"] # Add Sheet name
# File to be pasted into
template = openpyxl.load_workbook("my analyse document.xlsx") # Add file name
temp_sheet = template["Sheet2"] # Add Sheet name
# Copy range of cells as a nested list
# Takes: start cell, end cell, and sheet you want to copy from.
def copyRange(startCol, startRow, endCol, endRow, sheet):
rangeSelected = []
# Loops through selected Rows
for i in range(startRow, endRow + 1, 1):
# Appends the row to a RowSelected list
rowSelected = []
for j in range(startCol, endCol + 1, 1):
rowSelected.append(sheet.cell(row=i, column=j).value)
# Adds the RowSelected List and nests inside the rangeSelected
rangeSelected.append(rowSelected)
return rangeSelected
# Paste range
# Paste data from copyRange into template sheet
def pasteRange(startCol, startRow, endCol, endRow, sheetReceiving, copiedData):
countRow = 0
for i in range(startRow, endRow + 1, 1):
countCol = 0
for j in range(startCol, endCol + 1, 1):
sheetReceiving.cell(row=i, column=j).value = copiedData[countRow][countCol]
countCol += 1
countRow += 1
def createData():
print("Processing...")
selectedRange = copyRange(2, 2011, 183, 2274, sheet) # Change the 4 number values (startCol, startRow, endCol, endRow, sheet)
pastingRange = pasteRange(2, 4573, 182, 4836, temp_sheet, selectedRange) # Change the 4 number values (startCol, startRow, endCol, endRow, sheet)
# You can save the template as another file to create a new file here too.s
template.save("my analyse document.xlsx")
print("Range copied and pasted!")
go= createData()
是的,看看这里 docs:
import xlsxwriter
workbook = xlsxwriter.Workbook('your_file.xlsx')
worksheet = workbook.add_worksheet()
cell_format05 = workbook.add_format()
cell_format05.set_num_format('mm/dd/yy')
worksheet.write(5, 0, 36892.521, cell_format05) # output -> 01/01/01
# untested code for you, get your cells into column_D8_downwards array
# this is rather a structural code, not functional!
row = 0
for cell in column_D8_downwards:
worksheet.write(row, 'D8', cell, cell_format_05)
row=+1
workbook.close()
因此遍历列中的所有单元格(D8 向下)并将旧值写入新格式的单元格。
您也可以在 xlrd
的帮助下使用 pandas.DataFrame
读取数据后执行相同的操作:
import xlrd
import pandas as pd
df = pd.read_csv('Your_File.csv')
df = df[6:].reset_index()
df.columns = df.iloc[0]
df['Date'] = df['Date_Int'].apply(lambda x: xlrd.xldate.xldate_as_datetime(x, 0))
print(df)
TARİH Date
0 43891 2020-03-01
1 43892 2020-03-02
2 43893 2020-03-03
3 43894 2020-03-04
4 43895 2020-03-05
此外,您可以根据需要更改日期格式。
问题解决的很简单,解决的时候惊呆了
1-打开"my analyse document.xlsx"
2-Select整个"D"列
3-单击主页按钮
4-格式单元格-> 短日期
即使我用 python 更新 excel 数据,列格式类型也没有改变。
感谢大家的支持